Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directives to directive arguments #7130

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ internal sealed class DirectiveTypeFactory : ITypeFactory<DirectiveDefinitionNod

public DirectiveType Create(IDescriptorContext context, DirectiveDefinitionNode node)
{
var path = context.GetOrCreateDefinitionStack();
path.Clear();

var typeDefinition = new DirectiveTypeDefinition(
node.Name.Value,
node.Description?.Value,
Expand All @@ -104,16 +107,20 @@ public DirectiveType Create(IDescriptorContext context, DirectiveDefinitionNode
typeDefinition.IsPublic = true;
}

DeclareArguments(typeDefinition, node.Arguments);
DeclareArguments(context, typeDefinition, node.Arguments, path);
DeclareLocations(typeDefinition, node);

return DirectiveType.CreateUnsafe(typeDefinition);
}

private static void DeclareArguments(
IDescriptorContext context,
DirectiveTypeDefinition parent,
IReadOnlyCollection<InputValueDefinitionNode> arguments)
IReadOnlyCollection<InputValueDefinitionNode> arguments,
Stack<IDefinition> path)
{
path.Push(parent);

foreach (var argument in arguments)
{
var argumentDefinition = new DirectiveArgumentDefinition(
Expand All @@ -127,8 +134,12 @@ public DirectiveType Create(IDescriptorContext context, DirectiveDefinitionNode
argumentDefinition.DeprecationReason = reason;
}

SdlToTypeSystemHelper.AddDirectives(context, argumentDefinition, argument, path);

parent.Arguments.Add(argumentDefinition);
}

path.Pop();
}

private static void DeclareLocations(
Expand Down
38 changes: 33 additions & 5 deletions src/HotChocolate/Core/test/Types.Tests/Types/DirectiveTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void Use_DelegateMiddleware()
var directive = schema.GetDirectiveType("foo");
Assert.NotNull(directive.Middleware);
}

[Fact]
public async Task Use_EnsureClassMiddlewareDoesNotTrap_Next()
{
Expand All @@ -311,13 +311,13 @@ public async Task Use_EnsureClassMiddlewareDoesNotTrap_Next()
{
descriptor
.Name("Query");

descriptor
.Field("foo")
.Type<StringType>()
.Resolve("bar")
.Directive("foo");

descriptor
.Field("foo1")
.Type<IntType>()
Expand All @@ -340,7 +340,7 @@ public async Task Use_EnsureClassMiddlewareDoesNotTrap_Next()
await schema.MakeExecutable().ExecuteAsync("{ foo1 }");
await schema.MakeExecutable().ExecuteAsync("{ foo foo1 }");
var result = await schema.MakeExecutable().ExecuteAsync("{ foo foo1 }");

result.MatchSnapshot();
}

Expand Down Expand Up @@ -806,6 +806,34 @@ public void Directive_ValidateArgs_Overflow()
errors[0].Message.MatchSnapshot();
}

[Fact]
public async Task Directive_ArgumentDirective_AddedToSchema()
{
// arrange
// act
var schema = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(
x => x
.Name("Query")
.Field("bar")
.Resolve("asd")
.Directive("Qux"))
.AddDocumentFromString(
"""
directive @Example on ARGUMENT_DEFINITION
directive @Qux(bar: String @Example) on FIELD_DEFINITION
""")
.BuildSchemaAsync();

// assert
Assert.True(
schema.DirectiveTypes
.Single(d => d.Name == "Qux")
.Arguments["bar"]
.Directives[0].Type.Name == "Example");
}

[Fact]
public async Task AnnotationBased_Directive()
{
Expand Down Expand Up @@ -912,7 +940,7 @@ public DirectiveMiddleware(FieldDelegate next)
public Task InvokeAsync(IMiddlewareContext context) =>
Task.CompletedTask;
}

public class DirectiveMiddleware1
{
private readonly FieldDelegate _next;
Expand Down
Loading