-
-
Notifications
You must be signed in to change notification settings - Fork 803
Expand file tree
/
Copy pathFusionTestBase.CreateSourceSchema.cs
More file actions
129 lines (118 loc) · 5.3 KB
/
FusionTestBase.CreateSourceSchema.cs
File metadata and controls
129 lines (118 loc) · 5.3 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Collections.Immutable;
using System.Net.Http.Headers;
using HotChocolate.AspNetCore;
using HotChocolate.Configuration;
using HotChocolate.Execution.Configuration;
using HotChocolate.Fusion.Execution.Clients;
using HotChocolate.Types.Descriptors;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Composite = HotChocolate.Types.Composite;
namespace HotChocolate.Fusion;
public abstract partial class FusionTestBase
{
protected TestServer CreateSourceSchema(
string schemaName,
Action<IRequestExecutorBuilder> configureBuilder,
Action<IServiceCollection>? configureServices = null,
Action<IApplicationBuilder>? configureApplication = null,
Action<HttpClient>? configureHttpClient = null,
SourceSchemaHttpClientBatchingMode batchingMode =
SourceSchemaHttpClientBatchingMode.VariableBatching
| SourceSchemaHttpClientBatchingMode.RequestBatching,
ImmutableArray<MediaTypeWithQualityHeaderValue>? batchingAcceptHeaderValues = null,
bool isOffline = false,
bool isTimingOut = false)
{
configureApplication ??=
app =>
{
app.UseWebSockets();
app.UseRouting();
app.UseEndpoints(endpoint =>
endpoint.MapGraphQL(schemaName: schemaName));
};
return _testServerSession.CreateServer(
services =>
{
services.AddRouting();
var builder = services.AddGraphQLServer(schemaName, disableDefaultSecurity: true);
builder.AddSourceSchemaDefaults();
builder.ModifyServerOptions(o => o.Batching = AllowedBatching.All);
configureBuilder(builder);
configureServices?.Invoke(services);
services.Configure<SourceSchemaOptions>(opt =>
{
opt.IsOffline = isOffline;
opt.IsTimingOut = isTimingOut;
opt.ConfigureHttpClient = configureHttpClient;
opt.BatchingMode = batchingMode;
opt.BatchingAcceptHeaderValues = batchingAcceptHeaderValues;
});
},
configureApplication);
}
protected TestServer CreateSourceSchema(
string schemaName,
string schemaText,
bool isOffline = false,
bool isTimingOut = false,
SourceSchemaHttpClientBatchingMode batchingMode =
SourceSchemaHttpClientBatchingMode.VariableBatching
| SourceSchemaHttpClientBatchingMode.RequestBatching,
ImmutableArray<MediaTypeWithQualityHeaderValue>? batchingAcceptHeaderValues = null,
Action<HttpClient>? configureHttpClient = null,
HttpClient? httpClient = null)
{
return _testServerSession.CreateServer(services =>
{
services.AddRouting();
services.AddGraphQLServer(schemaName, disableDefaultSecurity: true)
.AddType<Composite.FieldSelectionSetType>()
.AddType<Composite.FieldSelectionMapType>()
.TryAddTypeInterceptor<RegisterFusionDirectivesTypeInterceptor>()
.AddDocumentFromString(schemaText)
.AddResolverMocking()
.AddTestDirectives()
.ModifyServerOptions(o => o.Batching = AllowedBatching.All);
services.Configure<SourceSchemaOptions>(opt =>
{
opt.IsOffline = isOffline;
opt.IsTimingOut = isTimingOut;
opt.ConfigureHttpClient = configureHttpClient;
opt.HttpClient = httpClient;
opt.BatchingMode = batchingMode;
opt.BatchingAcceptHeaderValues = batchingAcceptHeaderValues;
});
},
app =>
{
app.UseRouting();
app.UseEndpoints(endpoint =>
endpoint.MapGraphQL(schemaName: schemaName));
});
}
// ReSharper disable once ClassNeverInstantiated.Local
protected sealed class RegisterFusionDirectivesTypeInterceptor : TypeInterceptor
{
private bool _registeredTypes;
public override IEnumerable<TypeReference> RegisterMoreTypes(
IReadOnlyCollection<ITypeDiscoveryContext> discoveryContexts)
{
if (!_registeredTypes)
{
var typeInspector = discoveryContexts.First().DescriptorContext.TypeInspector;
yield return typeInspector.GetTypeRef(typeof(Composite.Lookup));
yield return typeInspector.GetTypeRef(typeof(Composite.Internal));
yield return typeInspector.GetTypeRef(typeof(Composite.Inaccessible));
yield return typeInspector.GetTypeRef(typeof(Composite.EntityKey));
yield return typeInspector.GetTypeRef(typeof(Composite.Require));
yield return typeInspector.GetTypeRef(typeof(Composite.Is));
yield return typeInspector.GetTypeRef(typeof(Composite.Provides));
yield return typeInspector.GetTypeRef(typeof(Composite.Shareable));
_registeredTypes = true;
}
}
}
}