|
| 1 | +using System.Collections.Frozen; |
1 | 2 | using System.Diagnostics.CodeAnalysis;
|
2 | 3 | using HotChocolate.Execution.Configuration;
|
| 4 | +using HotChocolate.Fusion.Definitions; |
| 5 | +using HotChocolate.Fusion.Execution; |
3 | 6 | using HotChocolate.Fusion.Execution.Nodes;
|
4 | 7 | using HotChocolate.Fusion.Logging;
|
5 | 8 | using HotChocolate.Fusion.Planning;
|
6 | 9 | using HotChocolate.Fusion.Rewriters;
|
7 | 10 | using HotChocolate.Fusion.Types;
|
8 | 11 | using HotChocolate.Language;
|
| 12 | +using HotChocolate.Types; |
| 13 | +using HotChocolate.Types.Mutable; |
| 14 | +using HotChocolate.Types.Mutable.Serialization; |
9 | 15 | using Microsoft.AspNetCore.Builder;
|
10 | 16 | using Microsoft.AspNetCore.TestHost;
|
11 | 17 | using Microsoft.Extensions.DependencyInjection;
|
12 | 18 | using Microsoft.Extensions.ObjectPool;
|
| 19 | +using Directive = HotChocolate.Types.Mutable.Directive; |
13 | 20 |
|
14 | 21 | namespace HotChocolate.Fusion;
|
15 | 22 |
|
@@ -133,4 +140,85 @@ protected virtual void Dispose(bool disposing)
|
133 | 140 | _testServerSession.Dispose();
|
134 | 141 | }
|
135 | 142 | }
|
| 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 | + } |
136 | 224 | }
|
0 commit comments