Skip to content

Commit a395e67

Browse files
glen-84michaelstaib
authored andcommitted
Allowed built-in scalars and directives to be parsed by the SchemaParser (#7809)
1 parent a0207c0 commit a395e67

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

src/HotChocolate/Skimmed/src/Skimmed/Serialization/SchemaParser.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@ private static void DiscoverDirectives(SchemaDefinition schema, DocumentNode doc
3838
{
3939
if (definition is DirectiveDefinitionNode def)
4040
{
41-
if (BuiltIns.IsBuiltInDirective(def.Name.Value))
42-
{
43-
// If a built-in directive is redefined in the schema, we just ignore it.
44-
continue;
45-
}
46-
4741
if (schema.DirectiveDefinitions.ContainsName(def.Name.Value))
4842
{
4943
// TODO : parsing error
5044
throw new Exception("duplicate");
5145
}
5246

53-
schema.DirectiveDefinitions.Add(new DirectiveDefinition(def.Name.Value));
47+
schema.DirectiveDefinitions.Add(
48+
new DirectiveDefinition(def.Name.Value)
49+
{
50+
IsSpecDirective = BuiltIns.IsBuiltInDirective(def.Name.Value)
51+
});
5452
}
5553
}
5654
}
@@ -61,11 +59,6 @@ private static void DiscoverTypes(SchemaDefinition schema, DocumentNode document
6159
{
6260
if (definition is ITypeDefinitionNode typeDef)
6361
{
64-
if (BuiltIns.IsBuiltInScalar(typeDef.Name.Value))
65-
{
66-
continue;
67-
}
68-
6962
if (schema.Types.ContainsName(typeDef.Name.Value))
7063
{
7164
// TODO : parsing error
@@ -91,7 +84,11 @@ private static void DiscoverTypes(SchemaDefinition schema, DocumentNode document
9184
break;
9285

9386
case ScalarTypeDefinitionNode:
94-
schema.Types.Add(new ScalarTypeDefinition(typeDef.Name.Value));
87+
schema.Types.Add(
88+
new ScalarTypeDefinition(typeDef.Name.Value)
89+
{
90+
IsSpecScalar = BuiltIns.IsBuiltInScalar(typeDef.Name.Value)
91+
});
9592
break;
9693

9794
case UnionTypeDefinitionNode:
@@ -196,11 +193,6 @@ private static void BuildTypes(SchemaDefinition schema, DocumentNode document)
196193
break;
197194

198195
case ScalarTypeDefinitionNode typeDef:
199-
if (BuiltIns.IsBuiltInScalar(typeDef.Name.Value))
200-
{
201-
continue;
202-
}
203-
204196
BuildScalarType(
205197
schema,
206198
(ScalarTypeDefinition)schema.Types[typeDef.Name.Value],
@@ -545,11 +537,6 @@ private static void BuildDirectiveTypes(SchemaDefinition schema, DocumentNode do
545537
{
546538
if (definition is DirectiveDefinitionNode directiveDef)
547539
{
548-
if (BuiltIns.IsBuiltInDirective(directiveDef.Name.Value))
549-
{
550-
continue;
551-
}
552-
553540
BuildDirectiveType(
554541
schema,
555542
schema.DirectiveDefinitions[directiveDef.Name.Value],

src/HotChocolate/Skimmed/test/Skimmed.Tests/SchemaParserTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
22
using HotChocolate.Skimmed.Serialization;
3+
using HotChocolate.Types;
34

45
namespace HotChocolate.Skimmed;
56

@@ -118,4 +119,46 @@ extend type Foo {
118119
});
119120
});
120121
}
122+
123+
[Fact]
124+
public void Parse_With_Custom_BuiltIn_Scalar_Type()
125+
{
126+
// arrange
127+
var sdl =
128+
"""
129+
"Custom description"
130+
scalar String @custom
131+
""";
132+
133+
// act
134+
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
135+
var scalar = schema.Types["String"];
136+
137+
// assert
138+
Assert.Equal("Custom description", scalar.Description);
139+
Assert.True(scalar.Directives.ContainsName("custom"));
140+
}
141+
142+
[Fact]
143+
public void Parse_With_Custom_BuiltIn_Directive()
144+
{
145+
// arrange
146+
var sdl =
147+
"""
148+
"Custom description"
149+
directive @skip("Custom argument description" ifCustom: String! @custom) on ENUM_VALUE
150+
""";
151+
152+
// act
153+
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
154+
var directive = schema.DirectiveDefinitions["skip"];
155+
var argument = directive.Arguments["ifCustom"];
156+
157+
// assert
158+
Assert.Equal("Custom description", directive.Description);
159+
Assert.Equal("Custom argument description", argument.Description);
160+
Assert.Equal("String", argument.Type.NamedType().Name);
161+
Assert.True(argument.Directives.ContainsName("custom"));
162+
Assert.Equal(DirectiveLocation.EnumValue, directive.Locations);
163+
}
121164
}

0 commit comments

Comments
 (0)