Skip to content

Commit d474cae

Browse files
tobias-tenglerJesseXia
authored andcommitted
[Fusion] Abstract type & global object identification tests (ChilliCream#8284)
1 parent 57485d1 commit d474cae

File tree

5 files changed

+5070
-0
lines changed

5 files changed

+5070
-0
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/HotChocolate.Fusion.Composition.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<InternalsVisibleTo Include="HotChocolate.Fusion.Composition.Tests" />
10+
<InternalsVisibleTo Include="HotChocolate.Fusion.Execution.Tests" />
1011
</ItemGroup>
1112

1213
<ItemGroup>

src/HotChocolate/Fusion-vnext/test/Fusion.Execution.Tests/FusionTestBase.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
using System.Collections.Frozen;
12
using System.Diagnostics.CodeAnalysis;
23
using HotChocolate.Execution.Configuration;
4+
using HotChocolate.Fusion.Definitions;
5+
using HotChocolate.Fusion.Execution;
36
using HotChocolate.Fusion.Execution.Nodes;
47
using HotChocolate.Fusion.Logging;
58
using HotChocolate.Fusion.Planning;
69
using HotChocolate.Fusion.Rewriters;
710
using HotChocolate.Fusion.Types;
811
using HotChocolate.Language;
12+
using HotChocolate.Types;
13+
using HotChocolate.Types.Mutable;
14+
using HotChocolate.Types.Mutable.Serialization;
915
using Microsoft.AspNetCore.Builder;
1016
using Microsoft.AspNetCore.TestHost;
1117
using Microsoft.Extensions.DependencyInjection;
1218
using Microsoft.Extensions.ObjectPool;
19+
using Directive = HotChocolate.Types.Mutable.Directive;
1320

1421
namespace HotChocolate.Fusion;
1522

@@ -133,4 +140,85 @@ protected virtual void Dispose(bool disposing)
133140
_testServerSession.Dispose();
134141
}
135142
}
143+
144+
protected record TestSubgraph([StringSyntax("graphql")] string Schema);
145+
146+
protected class TestSubgraphCollection(params TestSubgraph[] subgraphs)
147+
{
148+
public FusionSchemaDefinition BuildFusionSchema()
149+
{
150+
var schemas = subgraphs.Select(s => s.Schema).ToArray();
151+
var rewrittenSchemas = new string[schemas.Length];
152+
153+
for (var i = 0; i < schemas.Length; i++)
154+
{
155+
var sourceSchema = SchemaParser.Parse(schemas[i]);
156+
157+
AddFusionDirectives(sourceSchema);
158+
159+
if (sourceSchema.Name == "default")
160+
{
161+
sourceSchema.Name = $"Subgraph_{i + 1}";
162+
163+
if (sourceSchema.DirectiveDefinitions.TryGetDirective(WellKnownDirectiveNames.SchemaName,
164+
out var schemaNameDirectiveDefinition))
165+
{
166+
sourceSchema.Directives.Add(new Directive(schemaNameDirectiveDefinition,
167+
new ArgumentAssignment(WellKnownArgumentNames.Value, sourceSchema.Name)));
168+
}
169+
}
170+
171+
rewrittenSchemas[i] = sourceSchema.ToString();
172+
}
173+
174+
return ComposeSchema(rewrittenSchemas);
175+
}
176+
177+
private static void AddFusionDirectives(MutableSchemaDefinition schema)
178+
{
179+
var fieldSelectionMapType = MutableScalarTypeDefinition.Create(WellKnownTypeNames.FieldSelectionMap);
180+
var fieldSelectionSetType = MutableScalarTypeDefinition.Create(WellKnownTypeNames.FieldSelectionSet);
181+
var stringType = BuiltIns.String.Create();
182+
183+
var fusionDirectives = new Dictionary<string, MutableDirectiveDefinition>()
184+
{
185+
{ "external", new ExternalMutableDirectiveDefinition() },
186+
{ "inaccessible", new InaccessibleMutableDirectiveDefinition() },
187+
{ "internal", new InternalMutableDirectiveDefinition() },
188+
{ "is", new IsMutableDirectiveDefinition(fieldSelectionMapType) },
189+
{ "key", new KeyMutableDirectiveDefinition(fieldSelectionSetType) },
190+
{ "lookup", new LookupMutableDirectiveDefinition() },
191+
{ "override", new OverrideMutableDirectiveDefinition(stringType) },
192+
{ "provides", new ProvidesMutableDirectiveDefinition(fieldSelectionSetType) },
193+
{ "require", new RequireMutableDirectiveDefinition(fieldSelectionMapType) },
194+
{ "schemaName", new SchemaNameMutableDirectiveDefinition(stringType) },
195+
{ "shareable", new ShareableMutableDirectiveDefinition() }
196+
};
197+
198+
if (schema.Types.TryGetType(fieldSelectionMapType.Name, out var existingFieldSelectionMapType))
199+
{
200+
schema.Types.Remove(existingFieldSelectionMapType);
201+
}
202+
203+
schema.Types.Add(fieldSelectionMapType);
204+
205+
if (schema.Types.TryGetType(fieldSelectionSetType.Name, out var existingFieldSelectionSetType))
206+
{
207+
schema.Types.Remove(existingFieldSelectionSetType);
208+
}
209+
210+
schema.Types.Add(fieldSelectionSetType);
211+
212+
foreach (var fusionDirective in fusionDirectives.Values)
213+
{
214+
if (schema.DirectiveDefinitions.TryGetDirective(fusionDirective.Name,
215+
out var existingDirectiveDefinition))
216+
{
217+
schema.DirectiveDefinitions.Remove(existingDirectiveDefinition);
218+
}
219+
220+
schema.DirectiveDefinitions.Add(fusionDirective);
221+
}
222+
}
223+
}
136224
}

0 commit comments

Comments
 (0)