Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated Gateway into Request Pipeline (#5298)
- Loading branch information
1 parent
337af7a
commit 9f44720
Showing
26 changed files
with
392 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,5 +221,3 @@ public int Compare(IError? x, IError? y) | |
public static readonly ErrorComparer Default = new(); | ||
} | ||
} | ||
|
||
|
14 changes: 14 additions & 0 deletions
14
src/HotChocolate/Fusion/src/Core/Clients/GraphQLClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace HotChocolate.Fusion.Clients; | ||
|
||
internal sealed class GraphQLClientFactory | ||
{ | ||
private readonly Dictionary<string, IGraphQLClient> _executors; | ||
|
||
public GraphQLClientFactory(IEnumerable<IGraphQLClient> executors) | ||
{ | ||
_executors = executors.ToDictionary(t => t.SchemaName); | ||
} | ||
|
||
public IGraphQLClient Create(string schemaName) | ||
=> _executors[schemaName]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...late/Fusion/src/Core/Execution/Request.cs → ...Fusion/src/Core/Clients/GraphQLRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/HotChocolate/Fusion/src/Core/Clients/GraphQLResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Text.Json; | ||
|
||
namespace HotChocolate.Fusion.Clients; | ||
|
||
public sealed class GraphQLResponse : IDisposable | ||
{ | ||
private readonly JsonDocument? _document; | ||
|
||
public GraphQLResponse(JsonDocument? document) | ||
{ | ||
_document = document; | ||
|
||
if (_document is not null) | ||
{ | ||
if (_document.RootElement.TryGetProperty(ResponseProperties.Data, out var value)) | ||
{ | ||
Data = value; | ||
} | ||
|
||
if (_document.RootElement.TryGetProperty(ResponseProperties.Errors, out value)) | ||
{ | ||
Errors = value; | ||
} | ||
|
||
if (_document.RootElement.TryGetProperty(ResponseProperties.Extensions, out value)) | ||
{ | ||
Extensions = value; | ||
} | ||
} | ||
} | ||
|
||
public JsonElement Data { get; } | ||
|
||
public JsonElement Errors { get; } | ||
|
||
public JsonElement Extensions { get; } | ||
|
||
public void Dispose() | ||
{ | ||
_document?.Dispose(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/HotChocolate/Fusion/src/Core/Clients/IGraphQLClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace HotChocolate.Fusion.Clients; | ||
|
||
public interface IGraphQLClient | ||
{ | ||
string SchemaName { get; } | ||
|
||
Task<GraphQLResponse> ExecuteAsync( | ||
GraphQLRequest request, | ||
CancellationToken cancellationToken); | ||
|
||
Task<IAsyncEnumerable<GraphQLResponse>> ExecuteBatchAsync( | ||
IReadOnlyList<GraphQLRequest> requests, | ||
CancellationToken cancellationToken); | ||
|
||
Task<IAsyncEnumerable<GraphQLResponse>> SubscribeAsync( | ||
GraphQLRequest graphQLRequests, | ||
CancellationToken cancellationToken); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/HotChocolate/Fusion/src/Core/Clients/ResponseProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace HotChocolate.Fusion.Clients; | ||
|
||
internal static class ResponseProperties | ||
{ | ||
public const string Data = "data"; | ||
public const string Errors = "errors"; | ||
public const string Extensions = "extensions"; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/HotChocolate/Fusion/src/Core/DependencyInjection/RequestExecutorBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using HotChocolate.Execution.Configuration; | ||
using HotChocolate.Fusion.Clients; | ||
using HotChocolate.Fusion.Execution; | ||
using HotChocolate.Fusion.Metadata; | ||
using HotChocolate.Fusion.Pipeline; | ||
using HotChocolate.Fusion.Planning; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class RequestExecutorBuilderExtensions | ||
{ | ||
public static IRequestExecutorBuilder AddGraphQLGateway( | ||
this IRequestExecutorBuilder builder, | ||
string serviceConfig, | ||
string sdl) | ||
{ | ||
var configuration = ServiceConfiguration.Load(serviceConfig); | ||
|
||
return builder | ||
.AddDocumentFromString(sdl) | ||
.UseField(next => next) | ||
.UseDefaultGatewayPipeline() | ||
.ConfigureSchemaServices( | ||
sc => | ||
{ | ||
foreach (var schemaName in configuration.Bindings) | ||
{ | ||
sc.AddSingleton<IGraphQLClient>( | ||
sp => new GraphQLHttpClient( | ||
schemaName, | ||
sp.GetApplicationService<IHttpClientFactory>())); | ||
} | ||
sc.TryAddSingleton(configuration); | ||
sc.TryAddSingleton<RequestPlaner>(); | ||
sc.TryAddSingleton<RequirementsPlaner>(); | ||
sc.TryAddSingleton<ExecutionPlanBuilder>(); | ||
sc.TryAddSingleton<GraphQLClientFactory>(); | ||
sc.TryAddSingleton<FederatedQueryExecutor>(); | ||
}); | ||
} | ||
|
||
public static IRequestExecutorBuilder UseDefaultGatewayPipeline( | ||
this IRequestExecutorBuilder builder) | ||
{ | ||
return builder | ||
.UseInstrumentations() | ||
.UseExceptions() | ||
.UseTimeout() | ||
.UseDocumentCache() | ||
.UseDocumentParser() | ||
.UseDocumentValidation() | ||
.UseOperationCache() | ||
.UseOperationComplexityAnalyzer() | ||
.UseOperationResolver() | ||
.UseOperationVariableCoercion() | ||
.UseRequest<QueryPlanMiddleware>() | ||
.UseRequest<OperationExecutionMiddleware>(); | ||
} | ||
} |
Oops, something went wrong.