Skip to content

Commit 914577c

Browse files
[Fusion] Added built-in types and directives to source schemas (#8456)
Co-authored-by: Michael Staib <[email protected]>
1 parent 3569b38 commit 914577c

File tree

14 files changed

+161
-115
lines changed

14 files changed

+161
-115
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Extensions/MutableSchemaDefinitionExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ namespace HotChocolate.Fusion.Extensions;
55

66
internal static class MutableSchemaDefinitionExtensions
77
{
8+
public static void AddBuiltInFusionTypes(this MutableSchemaDefinition schema)
9+
{
10+
foreach (var builtInScalar in FusionBuiltIns.SourceSchemaScalars)
11+
{
12+
schema.Types.Add(builtInScalar);
13+
}
14+
}
15+
16+
public static void AddBuiltInFusionDirectives(this MutableSchemaDefinition schema)
17+
{
18+
foreach (var builtInDirective in FusionBuiltIns.SourceSchemaDirectives)
19+
{
20+
schema.DirectiveDefinitions.Add(builtInDirective);
21+
}
22+
}
23+
824
public static bool IsRootOperationType(
925
this MutableSchemaDefinition schema,
1026
MutableObjectTypeDefinition type)

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/FusionBuiltIns.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1+
using System.Collections.Frozen;
2+
using HotChocolate.Fusion.Definitions;
3+
using HotChocolate.Types.Mutable;
14
using static HotChocolate.Fusion.WellKnownTypeNames;
25

36
namespace HotChocolate.Fusion;
47

58
internal static class FusionBuiltIns
69
{
10+
private static readonly MutableScalarTypeDefinition s_fieldSelectionMapType =
11+
MutableScalarTypeDefinition.Create(FieldSelectionMap);
12+
13+
private static readonly MutableScalarTypeDefinition s_fieldSelectionSetType =
14+
MutableScalarTypeDefinition.Create(FieldSelectionSet);
15+
16+
private static readonly MutableScalarTypeDefinition s_stringType = BuiltIns.String.Create();
17+
18+
public static FrozenSet<MutableDirectiveDefinition> SourceSchemaDirectives { get; } =
19+
new HashSet<MutableDirectiveDefinition>(
20+
[
21+
new ExternalMutableDirectiveDefinition(),
22+
new InaccessibleMutableDirectiveDefinition(),
23+
new InternalMutableDirectiveDefinition(),
24+
new IsMutableDirectiveDefinition(s_fieldSelectionMapType),
25+
new KeyMutableDirectiveDefinition(s_fieldSelectionSetType),
26+
new LookupMutableDirectiveDefinition(),
27+
new OverrideMutableDirectiveDefinition(s_stringType),
28+
new ProvidesMutableDirectiveDefinition(s_fieldSelectionSetType),
29+
new RequireMutableDirectiveDefinition(s_fieldSelectionMapType),
30+
new SchemaNameMutableDirectiveDefinition(s_stringType),
31+
new ShareableMutableDirectiveDefinition()
32+
]).ToFrozenSet();
33+
34+
public static FrozenSet<MutableScalarTypeDefinition> SourceSchemaScalars { get; } =
35+
new HashSet<MutableScalarTypeDefinition>([s_fieldSelectionMapType, s_fieldSelectionSetType])
36+
.ToFrozenSet();
37+
738
public static bool IsBuiltInSourceSchemaScalar(string typeName)
839
{
940
return typeName switch

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/SourceSchemaParser.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Immutable;
22
using HotChocolate.Fusion.Comparers;
33
using HotChocolate.Fusion.Errors;
4+
using HotChocolate.Fusion.Extensions;
45
using HotChocolate.Fusion.Logging;
56
using HotChocolate.Fusion.Logging.Contracts;
67
using HotChocolate.Fusion.Results;
@@ -23,7 +24,17 @@ public CompositionResult<ImmutableSortedSet<MutableSchemaDefinition>> Parse()
2324
{
2425
try
2526
{
26-
var schema = SchemaParser.Parse(sourceSchema);
27+
var schema = new MutableSchemaDefinition();
28+
schema.AddBuiltInFusionTypes();
29+
schema.AddBuiltInFusionDirectives();
30+
SchemaParser.Parse(
31+
schema,
32+
sourceSchema,
33+
new SchemaParserOptions
34+
{
35+
IgnoreExistingTypes = true,
36+
IgnoreExistingDirectives = true
37+
});
2738
var schemaNameDirective = schema.Directives.FirstOrDefault(SchemaName);
2839

2940
if (schema.Name == "default"
Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
using System.Collections.Frozen;
2-
using HotChocolate.Fusion.Definitions;
31
using HotChocolate.Fusion.Events;
42
using HotChocolate.Fusion.Events.Contracts;
53
using HotChocolate.Types;
6-
using HotChocolate.Types.Mutable;
74
using static HotChocolate.Fusion.Logging.LogEntryHelper;
85
using static HotChocolate.Fusion.Properties.CompositionResources;
9-
using static HotChocolate.Fusion.WellKnownTypeNames;
106

117
namespace HotChocolate.Fusion.SourceSchemaValidationRules;
128

@@ -26,28 +22,28 @@ public void Handle(SchemaEvent @event, CompositionContext context)
2622
{
2723
var schema = @event.Schema;
2824

29-
// Types.
30-
if (schema.Types.TryGetType(FieldSelectionMap, out var fieldSelectionMapType)
31-
&& fieldSelectionMapType.Kind != TypeKind.Scalar)
25+
// Scalars.
26+
foreach (var builtInScalar in FusionBuiltIns.SourceSchemaScalars)
3227
{
33-
context.Log.Write(TypeDefinitionInvalid(fieldSelectionMapType, schema));
34-
}
35-
36-
if (schema.Types.TryGetType(FieldSelectionSet, out var fieldSelectionSetType)
37-
&& fieldSelectionSetType.Kind != TypeKind.Scalar)
38-
{
39-
context.Log.Write(TypeDefinitionInvalid(fieldSelectionSetType, schema));
28+
if (schema.Types.TryGetType(builtInScalar.Name, out var type)
29+
&& type.Kind != TypeKind.Scalar)
30+
{
31+
context.Log.Write(TypeDefinitionInvalid(type, schema));
32+
}
4033
}
4134

4235
// Directives.
43-
foreach (var (name, definition) in _builtInDirectives)
36+
foreach (var builtInDirective in FusionBuiltIns.SourceSchemaDirectives)
4437
{
45-
if (!schema.DirectiveDefinitions.TryGetDirective(name, out var directive))
38+
if (
39+
!schema.DirectiveDefinitions.TryGetDirective(
40+
builtInDirective.Name,
41+
out var directive))
4642
{
4743
continue;
4844
}
4945

50-
foreach (var expectedArgument in definition.Arguments)
46+
foreach (var expectedArgument in builtInDirective.Arguments)
5147
{
5248
var argumentName = expectedArgument.Name;
5349

@@ -79,30 +75,4 @@ public void Handle(SchemaEvent @event, CompositionContext context)
7975
}
8076
}
8177
}
82-
83-
private readonly FrozenDictionary<string, MutableDirectiveDefinition> _builtInDirectives =
84-
CreateBuiltInDirectiveDefinitions();
85-
86-
private static FrozenDictionary<string, MutableDirectiveDefinition>
87-
CreateBuiltInDirectiveDefinitions()
88-
{
89-
var fieldSelectionMapType = MutableScalarTypeDefinition.Create(FieldSelectionMap);
90-
var fieldSelectionSetType = MutableScalarTypeDefinition.Create(FieldSelectionSet);
91-
var stringType = BuiltIns.String.Create();
92-
93-
return new Dictionary<string, MutableDirectiveDefinition>()
94-
{
95-
{ "external", new ExternalMutableDirectiveDefinition() },
96-
{ "inaccessible", new InaccessibleMutableDirectiveDefinition() },
97-
{ "internal", new InternalMutableDirectiveDefinition() },
98-
{ "is", new IsMutableDirectiveDefinition(fieldSelectionMapType) },
99-
{ "key", new KeyMutableDirectiveDefinition(fieldSelectionSetType) },
100-
{ "lookup", new LookupMutableDirectiveDefinition() },
101-
{ "override", new OverrideMutableDirectiveDefinition(stringType) },
102-
{ "provides", new ProvidesMutableDirectiveDefinition(fieldSelectionSetType) },
103-
{ "require", new RequireMutableDirectiveDefinition(fieldSelectionMapType) },
104-
{ "schemaName", new SchemaNameMutableDirectiveDefinition(stringType) },
105-
{ "shareable", new ShareableMutableDirectiveDefinition() }
106-
}.ToFrozenDictionary();
107-
}
10878
}

src/HotChocolate/Fusion-vnext/test/Fusion.CommandLine.Tests/__resources__/invalid-example-1/source-schema-1.graphqls

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ type Query {
99
type User {
1010
id: ID!
1111
}
12-
13-
directive @schemaName(value: String!) on SCHEMA
14-
directive @lookup on FIELD_DEFINITION

src/HotChocolate/Fusion-vnext/test/Fusion.CommandLine.Tests/__resources__/invalid-example-1/source-schema-2.graphqls

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,3 @@ type User {
1010
id: ID!
1111
username: String!
1212
}
13-
14-
directive @schemaName(value: String!) on SCHEMA
15-
directive @lookup on FIELD_DEFINITION

src/HotChocolate/Fusion-vnext/test/Fusion.CommandLine.Tests/__resources__/invalid-example-2/source-schema-1.graphqls

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,3 @@ type Query @inaccessible { # Error: QUERY_ROOT_TYPE_INACCESSIBLE
99
type User {
1010
id: ID!
1111
}
12-
13-
directive @schemaName(value: String!) on SCHEMA
14-
directive @inaccessible on OBJECT # | etc.
15-
directive @lookup on FIELD_DEFINITION

src/HotChocolate/Fusion-vnext/test/Fusion.CommandLine.Tests/__resources__/invalid-example-2/source-schema-2.graphqls

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ type Query @inaccessible { # Error: QUERY_ROOT_TYPE_IN
99
type User {
1010
username: String!
1111
}
12-
13-
directive @schemaName(value: String!) on SCHEMA
14-
directive @lookup on FIELD_DEFINITION

src/HotChocolate/Fusion-vnext/test/Fusion.CommandLine.Tests/__resources__/valid-example-1/source-schema-1.graphqls

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ schema @schemaName(value: "Schema1") {
55
type Query {
66
schema1Field: Int!
77
}
8-
9-
directive @schemaName(value: String!) on SCHEMA

src/HotChocolate/Fusion-vnext/test/Fusion.CommandLine.Tests/__resources__/valid-example-1/source-schema-2.graphqls

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ schema @schemaName(value: "Schema2") {
55
type Query {
66
schema2Field: Int!
77
}
8-
9-
directive @schemaName(value: String!) on SCHEMA

0 commit comments

Comments
 (0)