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 v11 #2010

Merged
merged 3 commits into from Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,6 +6,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 @@ -42,7 +43,7 @@ public void Accept(ObjectField field)
}

public bool TryProject<T>(
[NotNullWhen(true)]out Expression<Func<T, T>>? projection)
[NotNullWhen(true)] out Expression<Func<T, T>>? projection)
{
if (_selectionMiddlewareContext.Errors.Count == 0)
{
Expand All @@ -56,6 +57,15 @@ public void Accept(ObjectField field)
}
}

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
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 @@ -257,7 +258,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
Expand Up @@ -1519,6 +1519,52 @@ public virtual void Execute_Selection_Nested_FilteringAndSorting()
});
}

[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);
});
}

public class Query
{
public IQueryable<Foo> Foos { get; }
Expand Down Expand Up @@ -1558,7 +1604,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
Expand Up @@ -661,6 +661,52 @@ public virtual void Execute_Selection_Object_Paging_Combined()
Assert.Null(x.Node.ObjectArray);
});
}

[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);
});
}

public class Query
{
Expand Down Expand Up @@ -701,7 +747,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
Expand Up @@ -1378,6 +1378,52 @@ public virtual void Execute_Selection_Nested_FilteringAndSorting()
});
}

[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);
});
}

public class Query
{
public IQueryable<Foo> Foos { get; }
Expand Down Expand Up @@ -1416,7 +1462,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
Expand Up @@ -1292,6 +1292,50 @@ public virtual void Execute_Selection_Nested_FilteringAndSorting()
});
}

[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);
},
x =>
{
Assert.Equal("bb", x.Bar);
Assert.Equal(0, x.Baz);
Assert.Null(x.Nested);
});
}

public class Query
{
public IQueryable<Foo> Foos { get; }
Expand Down Expand Up @@ -1327,7 +1371,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