Skip to content

Commit

Permalink
Fixed issue that caused errors when collecting fields on a directive …
Browse files Browse the repository at this point in the history
…context
  • Loading branch information
michaelstaib committed Jan 13, 2020
1 parent aa00092 commit 23537d3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
59 changes: 59 additions & 0 deletions src/Core/Core.Tests/Execution/ResolverContextTests.cs
Expand Up @@ -105,6 +105,54 @@ public async Task CollectFields()
list.Select(t => t.Selection.Name.Value).ToList().MatchSnapshot();
}

[Fact]
public async Task DirectiveContextCollectFields()
{
// arrange
var list = new List<IFieldSelection>();

ISchema schema = SchemaBuilder.New()
.AddDocumentFromString(
@"
type Query {
foo: Foo @my
}
type Foo {
bar: Bar @my
}
type Bar {
baz: String @my
}")
.AddDirectiveType<MyDirectiveType>()
.Use(next => context =>
{
if (context.Field.Type.NamedType() is ObjectType type)
{
foreach (IFieldSelection selection in context.CollectFields(type))
{
CollectSelections(context, selection, list);
}
}
return Task.CompletedTask;
})
.Create();

// act
await schema.MakeExecutable().ExecuteAsync(
@"{
foo {
bar {
baz
}
}
}");

// assert
list.Select(t => t.Selection.Name.Value).ToList().MatchSnapshot();
}

private static void CollectSelections(
IResolverContext context,
IFieldSelection selection,
Expand All @@ -124,5 +172,16 @@ public async Task CollectFields()
}
}
}

public class MyDirectiveType
: DirectiveType
{
protected override void Configure(IDirectiveTypeDescriptor descriptor)
{
descriptor.Name("my");
descriptor.Location(DirectiveLocation.FieldDefinition);
descriptor.Use(next => context => next(context));
}
}
}
}
@@ -0,0 +1,3 @@
[
"baz"
]
4 changes: 2 additions & 2 deletions src/Core/Core/Execution/Utilities/DirectiveContext.cs
Expand Up @@ -115,11 +115,11 @@ public object Result

public IReadOnlyList<IFieldSelection> CollectFields(
ObjectType typeContext, SelectionSetNode selectionSet) =>
_middlewareContext.CollectFields(typeContext, FieldSelection.SelectionSet);
_middlewareContext.CollectFields(typeContext, selectionSet);

public IReadOnlyList<IFieldSelection> CollectFields(
ObjectType typeContext, SelectionSetNode selectionSet, Path path) =>
_middlewareContext.CollectFields(typeContext, FieldSelection.SelectionSet, path);
_middlewareContext.CollectFields(typeContext, selectionSet, path);

public ValueKind ArgumentKind(NameString name) =>
_middlewareContext.ArgumentKind(name);
Expand Down

0 comments on commit 23537d3

Please sign in to comment.