Skip to content

Commit

Permalink
Fix SchemaBuilder so it reads directives from extension types (#3667)
Browse files Browse the repository at this point in the history
* Fix SchemaBuilder so it reads directives from extension types

* Update src/GraphQL/Utilities/SchemaBuilderExtensions.cs
  • Loading branch information
Shane32 committed Jul 23, 2023
1 parent ad9286e commit a7b144b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/GraphQL.Tests/Utilities/SchemaBuilderTests.cs
Expand Up @@ -822,6 +822,29 @@ public void build_extension_type_out_of_order()
type.Fields.Count.ShouldBe(2);
}

[Fact]
public void reads_directives_from_types_or_extension_types()
{
var schema = Schema.For("""
extend type Query @directiveA {
field1: String
}

type Query @directiveB {
field2: String
}

directive @directiveA on OBJECT
directive @directiveB on OBJECT
""");

schema.Initialize();
var type = schema.AllTypes["Query"].ShouldNotBeNull();
var directives = type.GetAppliedDirectives()?.List.ShouldNotBeNull();
directives.Where(x => x.Name == "directiveA").ShouldHaveSingleItem();
directives.Where(x => x.Name == "directiveB").ShouldHaveSingleItem();
}

[Fact]
public async Task builds_with_customized_clr_type()
{
Expand Down
22 changes: 17 additions & 5 deletions src/GraphQL/Utilities/SchemaBuilderExtensions.cs
Expand Up @@ -35,9 +35,18 @@ public static bool AstTypeHasFields(this IProvideMetadata type)
{
provider.WithMetadata(AST_METAFIELD, node); //TODO: remove?

if (node is IHasDirectivesNode ast && ast.Directives?.Count > 0)
if (node is IHasDirectivesNode ast)
provider.CopyDirectivesFrom(ast);

return provider;
}

public static TMetadataProvider CopyDirectivesFrom<TMetadataProvider>(this TMetadataProvider provider, IHasDirectivesNode node)
where TMetadataProvider : IProvideMetadata
{
if (node.Directives?.Count > 0)
{
foreach (var directive in ast.Directives!)
foreach (var directive in node.Directives)
{
provider.ApplyDirective(directive!.Name.StringValue, d => //ISSUE:allocation
{
Expand All @@ -49,20 +58,23 @@ public static bool AstTypeHasFields(this IProvideMetadata type)
});
}
}

return provider;
}

public static bool HasExtensionAstTypes(this IProvideMetadata type)
{
return GetExtensionAstTypes(type).Count > 0;
return type.HasMetadata(EXTENSION_AST_METAFIELD) && GetExtensionAstTypes(type).Count > 0;
}

public static void AddExtensionAstType<T>(this IProvideMetadata type, T astType) where T : ASTNode
public static void AddExtensionAstType<T>(this IProvideMetadata type, T astType)
where T : ASTNode
{
var types = GetExtensionAstTypes(type);
types.Add(astType);
type.Metadata[EXTENSION_AST_METAFIELD] = types;

if (astType is IHasDirectivesNode ast)
type.CopyDirectivesFrom(ast);
}

public static List<ASTNode> GetExtensionAstTypes(this IProvideMetadata type)
Expand Down

0 comments on commit a7b144b

Please sign in to comment.