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

fix projection of __typename v10 #2009

Merged
merged 2 commits into from
Jun 3, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,52 @@ public virtual void Execute_Selection_Scalar()
});
}

[Fact]
public virtual void Execute_Selection_SingleScalarAndTypeName()
{
// arrange
IServiceCollection services;
Func<IResolverContext, IEnumerable<Foo>> resolver;
(services, resolver) = _provider.CreateResolver(SAMPLE);

IQueryable<Foo> resultCtx = null;
ISchema schema = SchemaBuilder.New()
.AddServices(services.BuildServiceProvider())
.AddQueryType<Query>(
d => d.Field(t => t.Foos)
.Resolver(resolver)
.Use(next => async ctx =>
{
await next(ctx).ConfigureAwait(false);
resultCtx = ctx.Result as IQueryable<Foo>;
})
.UseSelection())
.Create();
IQueryExecutor executor = schema.MakeExecutable();

// act
executor.Execute(
"{ foos { bar __typename } }");

// assert
Assert.NotNull(resultCtx);
Assert.Collection(resultCtx.ToArray(),
x =>
{
Assert.Equal("aa", x.Bar);
Assert.Equal(0, x.Baz);
Assert.Null(x.Nested);
Assert.Null(x.ObjectArray);
},
x =>
{
Assert.Equal("bb", x.Bar);
Assert.Equal(0, x.Baz);
Assert.Null(x.Nested);
Assert.Null(x.ObjectArray);
});
}

[Fact]
public virtual void Execute_Selection_MultipleScalar()
{
Expand Down Expand Up @@ -1522,7 +1568,7 @@ public class Foo

public string GetComputedField() => Bar + Baz;

public string GetComputedFieldParent([Parent]Foo foo) => foo.Bar + foo.Baz;
public string GetComputedFieldParent([Parent] Foo foo) => foo.Bar + foo.Baz;

public static Foo Create(string bar, int baz)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Types.Selection/SelectionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types.Introspection;
using HotChocolate.Types.Selections.Handlers;
using HotChocolate.Utilities;

Expand Down Expand Up @@ -40,6 +41,15 @@ public Expression<Func<T, T>> Project<T>()
return (Expression<Func<T, T>>)Closures.Peek().CreateMemberInitLambda();
}

protected override bool EnterLeaf(IFieldSelection selection)
{
if (IntrospectionFields.TypeName.Equals(selection.Field.Name))
{
return false;
}
return base.EnterLeaf(selection);
}

protected override void LeaveLeaf(IFieldSelection selection)
{
if (selection.Field.Member is PropertyInfo member)
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Types.Selection/SelectionVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types.Introspection;
using HotChocolate.Types.Relay;
using static HotChocolate.Utilities.DotNetTypeInfoFactory;

Expand Down Expand Up @@ -236,7 +237,8 @@ private static bool HasNonProjectableField(IReadOnlyList<IFieldSelection> select
{
for (int i = 0; i < selections.Count; i++)
{
if (!(selections[i].Field.Member is PropertyInfo))
if (!(selections[i].Field.Member is PropertyInfo) &&
!IntrospectionFields.TypeName.Equals(selections[i].Field.Name))
{
return true;
}
Expand Down