Skip to content

Commit

Permalink
Allowed built-in scalars and directives to be parsed by the SchemaPar…
Browse files Browse the repository at this point in the history
…ser (#7809)
  • Loading branch information
glen-84 authored Dec 9, 2024
1 parent 8bfa156 commit 3a848c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
33 changes: 10 additions & 23 deletions src/HotChocolate/Skimmed/src/Skimmed/Serialization/SchemaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,17 @@ private static void DiscoverDirectives(SchemaDefinition schema, DocumentNode doc
{
if (definition is DirectiveDefinitionNode def)
{
if (BuiltIns.IsBuiltInDirective(def.Name.Value))
{
// If a built-in directive is redefined in the schema, we just ignore it.
continue;
}

if (schema.DirectiveDefinitions.ContainsName(def.Name.Value))
{
// TODO : parsing error
throw new Exception("duplicate");
}

schema.DirectiveDefinitions.Add(new DirectiveDefinition(def.Name.Value));
schema.DirectiveDefinitions.Add(
new DirectiveDefinition(def.Name.Value)
{
IsSpecDirective = BuiltIns.IsBuiltInDirective(def.Name.Value)
});
}
}
}
Expand All @@ -61,11 +59,6 @@ private static void DiscoverTypes(SchemaDefinition schema, DocumentNode document
{
if (definition is ITypeDefinitionNode typeDef)
{
if (BuiltIns.IsBuiltInScalar(typeDef.Name.Value))
{
continue;
}

if (schema.Types.ContainsName(typeDef.Name.Value))
{
// TODO : parsing error
Expand All @@ -91,7 +84,11 @@ private static void DiscoverTypes(SchemaDefinition schema, DocumentNode document
break;

case ScalarTypeDefinitionNode:
schema.Types.Add(new ScalarTypeDefinition(typeDef.Name.Value));
schema.Types.Add(
new ScalarTypeDefinition(typeDef.Name.Value)
{
IsSpecScalar = BuiltIns.IsBuiltInScalar(typeDef.Name.Value)
});
break;

case UnionTypeDefinitionNode:
Expand Down Expand Up @@ -196,11 +193,6 @@ private static void BuildTypes(SchemaDefinition schema, DocumentNode document)
break;

case ScalarTypeDefinitionNode typeDef:
if (BuiltIns.IsBuiltInScalar(typeDef.Name.Value))
{
continue;
}

BuildScalarType(
schema,
(ScalarTypeDefinition)schema.Types[typeDef.Name.Value],
Expand Down Expand Up @@ -545,11 +537,6 @@ private static void BuildDirectiveTypes(SchemaDefinition schema, DocumentNode do
{
if (definition is DirectiveDefinitionNode directiveDef)
{
if (BuiltIns.IsBuiltInDirective(directiveDef.Name.Value))
{
continue;
}

BuildDirectiveType(
schema,
schema.DirectiveDefinitions[directiveDef.Name.Value],
Expand Down
43 changes: 43 additions & 0 deletions src/HotChocolate/Skimmed/test/Skimmed.Tests/SchemaParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using HotChocolate.Skimmed.Serialization;
using HotChocolate.Types;

namespace HotChocolate.Skimmed;

Expand Down Expand Up @@ -118,4 +119,46 @@ extend type Foo {
});
});
}

[Fact]
public void Parse_With_Custom_BuiltIn_Scalar_Type()
{
// arrange
var sdl =
"""
"Custom description"
scalar String @custom
""";

// act
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
var scalar = schema.Types["String"];

// assert
Assert.Equal("Custom description", scalar.Description);
Assert.True(scalar.Directives.ContainsName("custom"));
}

[Fact]
public void Parse_With_Custom_BuiltIn_Directive()
{
// arrange
var sdl =
"""
"Custom description"
directive @skip("Custom argument description" ifCustom: String! @custom) on ENUM_VALUE
""";

// act
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
var directive = schema.DirectiveDefinitions["skip"];
var argument = directive.Arguments["ifCustom"];

// assert
Assert.Equal("Custom description", directive.Description);
Assert.Equal("Custom argument description", argument.Description);
Assert.Equal("String", argument.Type.NamedType().Name);
Assert.True(argument.Directives.ContainsName("custom"));
Assert.Equal(DirectiveLocation.EnumValue, directive.Locations);
}
}

0 comments on commit 3a848c5

Please sign in to comment.