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: ConnectionMiddleware and IEnumerable + IConnection #2378

Merged
merged 4 commits into from
Sep 27, 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
120 changes: 120 additions & 0 deletions src/Core/Types.Tests/Types/Relay/ConnectionMiddlewareTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HotChocolate.Execution;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.Types.Relay
{
public class ConnectionMiddlewareTests
{
[Fact]
public async Task ExecuteQueryWithConnectionMiddleware_ShouldHandleVariousReturnTypes()
{
// arrange
var schema = Schema.Create(t =>
{
t.RegisterQueryType<SomeQuery>();
t.RegisterType<FooType>();
t.UseGlobalObjectIdentifier();
});

IQueryExecutor executor = schema.MakeExecutable();

// act
IExecutionResult result =
await executor.ExecuteAsync(
@"{
foosAsIConnectionAndIEnumerable(first: 2) {
totalCount
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
nodes { id string }
}
foosAsIEnumerable(first: 2) {
totalCount
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
nodes { id string }
}
foosAsIQueryable(first: 2) {
totalCount
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
nodes { id string }
}
}");

// assert
result.MatchSnapshot();
}

public class SomeQuery
{
[UsePaging(SchemaType = typeof(FooType))]
public FooConnection GetFoosAsIConnectionAndIEnumerable()
{
// Values returned here in this resolver should be considered as-is

var edges = Foos.Select(f => new Edge<Foo>(f, f.Id.ToString())).ToList();

var pageInfo = new PageInfo(
hasNextPage: true,
hasPreviousPage: true,
startCursor: edges.First().Cursor,
endCursor: edges.Last().Cursor,
totalCount: 100);

return new FooConnection(pageInfo, edges);
}

[UsePaging(SchemaType = typeof(FooType))]
public IEnumerable<Foo> GetFoosAsIEnumerable()
=> Foos;

[UsePaging(SchemaType = typeof(FooType))]
public IQueryable<Foo> GetFoosAsIQueryable()
=> Foos.AsQueryable();
}

private static IEnumerable<Foo> Foos => new List<Foo>
{
new Foo(1),
new Foo(2),
new Foo(3),
new Foo(4)
};

public class FooConnection : IConnection, IEnumerable<Foo>
{
public IPageInfo PageInfo { get; private set; }

public IReadOnlyList<IEdge> Edges { get; private set; }

public IEnumerator<Foo> GetEnumerator() => Edges.Select(e => e.Node).Cast<Foo>().GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public FooConnection(IPageInfo pageInfo, IReadOnlyList<IEdge> edges)
{
PageInfo = pageInfo;
Edges = edges;
}
}

public class Foo
{
[GraphQLType(typeof(NonNullType<IdType>))]
public int Id { get; private set; }

public string GetString() => "Hello";

public Foo(int id) => Id = id;
}

public class FooType : ObjectType<Foo>
{
protected override void Configure(IObjectTypeDescriptor<Foo> descriptor)
=> descriptor.AsNode().IdField(f => f.Id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"Data": {
"foosAsIConnectionAndIEnumerable": {
"totalCount": 100,
"pageInfo": {
"hasNextPage": true,
"hasPreviousPage": true,
"startCursor": "1",
"endCursor": "4"
},
"nodes": [
{
"id": "Rm9vCmkx",
"string": "Hello"
},
{
"id": "Rm9vCmky",
"string": "Hello"
},
{
"id": "Rm9vCmkz",
"string": "Hello"
},
{
"id": "Rm9vCmk0",
"string": "Hello"
}
]
},
"foosAsIEnumerable": {
"totalCount": 4,
"pageInfo": {
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "MA==",
"endCursor": "MQ=="
},
"nodes": [
{
"id": "Rm9vCmkx",
"string": "Hello"
},
{
"id": "Rm9vCmky",
"string": "Hello"
}
]
},
"foosAsIQueryable": {
"totalCount": 4,
"pageInfo": {
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "MA==",
"endCursor": "MQ=="
},
"nodes": [
{
"id": "Rm9vCmkx",
"string": "Hello"
},
{
"id": "Rm9vCmky",
"string": "Hello"
}
]
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
9 changes: 6 additions & 3 deletions src/Core/Types/Types/Relay/ConnectionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,7 +29,11 @@ public async Task InvokeAsync(
context.Argument<string>("after"),
context.Argument<string>("before"));

if (connectionResolver is { } && context.Result is TSource source)
if (context.Result is IConnection)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to look at the new implementation so that this will work with 11 as well.

{
// do nothing, we've already got an IConnection
}
else if (connectionResolver is { } && context.Result is TSource source)
{
context.Result = await connectionResolver.ResolveAsync(
context,
Expand Down