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

Fixed collect fields issue. #994

Merged
merged 1 commit into from
Aug 10, 2019
Merged
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
71 changes: 71 additions & 0 deletions src/Core/Core.Tests/Execution/ResolverContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.Threading.Tasks;
using Xunit;
using Snapshooter.Xunit;
using HotChocolate.Resolvers;
using System.Collections.Generic;
using HotChocolate.Types;
using System.Linq;

namespace HotChocolate.Execution
{
Expand Down Expand Up @@ -53,5 +57,72 @@ public async Task AccessVariables_Failes_When_Variable_Not_Exists()
// assert
result.MatchSnapshot();
}

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

ISchema schema = SchemaBuilder.New()
.AddDocumentFromString(
@"
type Query {
foo: Foo
}

type Foo {
bar: Bar
}

type Bar {
baz: String
}")
.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,
ICollection<IFieldSelection> collected)
{
if (selection.Field.Type.IsLeafType())
{
collected.Add(selection);
}

if (selection.Field.Type.NamedType() is ObjectType objectType)
{
foreach (IFieldSelection child in context.CollectFields(
objectType, selection.Selection.SelectionSet))
{
CollectSelections(context, child, collected);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"baz"
]
6 changes: 2 additions & 4 deletions src/Core/Core/Execution/Utilities/ResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,11 @@ public async Task<T> ResolveAsync<T>()

public IReadOnlyCollection<FieldSelection> CollectFields(
ObjectType typeContext) =>
_executionContext.CollectFields(
typeContext, FieldSelection.SelectionSet, Path);
CollectFields(typeContext, FieldSelection.SelectionSet);

public IReadOnlyCollection<FieldSelection> CollectFields(
ObjectType typeContext, SelectionSetNode selectionSet) =>
_executionContext.CollectFields(
typeContext, FieldSelection.SelectionSet, Path);
_executionContext.CollectFields(typeContext, selectionSet, Path);

IReadOnlyCollection<IFieldSelection> IResolverContext.CollectFields(
ObjectType typeContext) => CollectFields(typeContext);
Expand Down