Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fusion fetch in parallel #6306

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public DefaultHttpGraphQLClient(

public string SubgraphName => _config.SubgraphName;

public Task<GraphQLResponse> ExecuteAsync(
GraphQLRequest request,
CancellationToken cancellationToken)
public Task<GraphQLResponse> ExecuteAsync(GraphQLRequest request, CancellationToken cancellationToken)
{
if (request is null)
{
Expand All @@ -40,19 +38,15 @@ public Task<GraphQLResponse> ExecuteAsync(
return ExecuteInternalAsync(request, cancellationToken);
}

private async Task<GraphQLResponse> ExecuteInternalAsync(
GraphQLRequest request,
CancellationToken ct)
private async Task<GraphQLResponse> ExecuteInternalAsync(GraphQLRequest request, CancellationToken ct)
{
try
{
using var writer = new ArrayWriter();
using var requestMessage = CreateRequestMessage(writer, request);
using var responseMessage = await _client.SendAsync(requestMessage, ct);

await using var contentStream = await responseMessage.Content
.ReadAsStreamAsync(ct)
.ConfigureAwait(false);
await using var contentStream = await responseMessage.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);

var stream = contentStream;
var contentType = responseMessage.Content.Headers.ContentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using HotChocolate.Execution.Processing;
using HotChocolate.Fusion.Clients;
Expand Down Expand Up @@ -114,17 +115,20 @@ public async Task<IReadOnlyList<GraphQLResponse>> ExecuteAsync(
IReadOnlyList<GraphQLRequest> requests,
CancellationToken cancellationToken)
{
if(requests.Count == 1)
{
return new[] { await ExecuteAsync(subgraphName, requests[0], cancellationToken) };
}

await using var client = _clientFactory.CreateClient(subgraphName);
var responses = new GraphQLResponse[requests.Count];
var tasks = new Task<GraphQLResponse>[requests.Count];

for (var i = 0; i < requests.Count; i++)
{
responses[i] =
await client.ExecuteAsync(requests[i], cancellationToken)
.ConfigureAwait(false);
tasks[i] = client.ExecuteAsync(requests[i], cancellationToken);
}

return responses;
return await Task.WhenAll(tasks).ConfigureAwait(false);
}

public async IAsyncEnumerable<GraphQLResponse> SubscribeAsync(
Expand Down