|
| 1 | +using System.Collections.Frozen; |
1 | 2 | using System.Diagnostics.CodeAnalysis;
|
2 | 3 | using HotChocolate.Execution.Configuration;
|
| 4 | +using HotChocolate.Fusion.Definitions; |
3 | 5 | using HotChocolate.Fusion.Execution;
|
4 | 6 | using HotChocolate.Fusion.Execution.Nodes;
|
5 | 7 | using HotChocolate.Fusion.Logging;
|
6 | 8 | using HotChocolate.Fusion.Planning;
|
7 | 9 | using HotChocolate.Fusion.Rewriters;
|
8 | 10 | using HotChocolate.Fusion.Types;
|
9 | 11 | using HotChocolate.Language;
|
| 12 | +using HotChocolate.Types; |
| 13 | +using HotChocolate.Types.Mutable; |
| 14 | +using HotChocolate.Types.Mutable.Serialization; |
10 | 15 | using Microsoft.AspNetCore.Builder;
|
11 | 16 | using Microsoft.AspNetCore.TestHost;
|
12 | 17 | using Microsoft.Extensions.DependencyInjection;
|
13 | 18 | using Microsoft.Extensions.ObjectPool;
|
| 19 | +using Directive = HotChocolate.Types.Mutable.Directive; |
14 | 20 |
|
15 | 21 | namespace HotChocolate.Fusion;
|
16 | 22 |
|
@@ -142,8 +148,77 @@ protected class TestSubgraphCollection(params TestSubgraph[] subgraphs)
|
142 | 148 | public FusionSchemaDefinition BuildFusionSchema()
|
143 | 149 | {
|
144 | 150 | var schemas = subgraphs.Select(s => s.Schema).ToArray();
|
| 151 | + var rewrittenSchemas = new string[schemas.Length]; |
145 | 152 |
|
146 |
| - return ComposeSchema(schemas); |
| 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 | + } |
147 | 222 | }
|
148 | 223 | }
|
149 | 224 | }
|
0 commit comments