-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
RequestExecutorBuilderExtensions.cs
101 lines (90 loc) · 3.53 KB
/
RequestExecutorBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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 HotChocolate.Language;
using Microsoft.Extensions.DependencyInjection.Extensions;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;
public static class RequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder AddFusionGatewayServer(
this IServiceCollection services,
string serviceConfiguration)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
if (string.IsNullOrEmpty(serviceConfiguration))
{
throw new ArgumentNullException(nameof(serviceConfiguration));
}
var serviceConfDoc = Utf8GraphQLParser.Parse(serviceConfiguration);
return AddFusionGatewayServer(services, serviceConfDoc);
}
public static IRequestExecutorBuilder AddFusionGatewayServer(
this IServiceCollection services,
DocumentNode serviceConfiguration)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
if (serviceConfiguration is null)
{
throw new ArgumentNullException(nameof(serviceConfiguration));
}
var configuration = ServiceConfiguration.Load(serviceConfiguration);
var context = ConfigurationDirectiveNamesContext.From(serviceConfiguration);
var rewriter = new ServiceConfigurationToSchemaRewriter();
var schemaDoc = (DocumentNode?)rewriter.Rewrite(serviceConfiguration, context);
if (schemaDoc is null)
{
// todo : exception.
throw new InvalidOperationException(
"A valid service configuration must always produce a schema document.");
}
return services
.AddGraphQLServer()
.AddDocument(schemaDoc)
.UseField(next => next)
.UseDefaultGatewayPipeline()
.AddOperationCompilerOptimizer<OperationQueryPlanCompiler>()
.ConfigureSchemaServices(
sc =>
{
foreach (var schemaName in configuration.SchemaNames)
{
sc.AddSingleton<IGraphQLClient>(
sp => new GraphQLHttpClient(
schemaName,
sp.GetApplicationService<IHttpClientFactory>()));
}
sc.TryAddSingleton(configuration);
sc.TryAddSingleton<RequestPlanner>();
sc.TryAddSingleton<RequirementsPlanner>();
sc.TryAddSingleton<ExecutionPlanBuilder>();
sc.TryAddSingleton<GraphQLClientFactory>();
sc.TryAddSingleton<FederatedQueryExecutor>();
});
}
private static IRequestExecutorBuilder UseDefaultGatewayPipeline(
this IRequestExecutorBuilder builder)
{
return builder
.UseInstrumentations()
.UseExceptions()
.UseTimeout()
.UseDocumentCache()
.UseDocumentParser()
.UseDocumentValidation()
.UseOperationCache()
.UseOperationComplexityAnalyzer()
.UseOperationResolver()
.UseOperationVariableCoercion()
.UseRequest<OperationExecutionMiddleware>();
}
}