-
-
Notifications
You must be signed in to change notification settings - Fork 756
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b4c3594
fix: ConnectionMiddleware and IEnumerable + IConnection
benmccallum 9e51eed
feat: Unit test for specific case
benmccallum 5714465
feat: Added IEnumerable and IQueryable queries that show failure
benmccallum 17764b5
fix: Limit to first 2 to see better data for IEnumerable and IQueryab…
benmccallum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/Core/Types.Tests/Types/Relay/ConnectionMiddlewareTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...nMiddlewareTests.ExecuteQueryWithConnectionMiddleware_ShouldHandleVariousReturnTypes.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.