Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it easer to implement connections for new data sources (V10). (#…
- Loading branch information
1 parent
ae2c62d
commit 7e0b24b
Showing
39 changed files
with
1,042 additions
and
588 deletions.
There are no files selected for viewing
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
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
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
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
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Snapshooter.Xunit; | ||
using Xunit; | ||
|
||
|
@@ -163,6 +167,189 @@ public async Task UsePaging_WithComplexType() | |
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public void InferSchemaWithAttributesCorrectly() | ||
{ | ||
SchemaBuilder.New() | ||
.AddQueryType<QueryWithPagingAttribute>() | ||
.Create() | ||
.ToString() | ||
.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task UsePagingAttribute_InMemory_Collection() | ||
{ | ||
// arrange | ||
ISchema schema = SchemaBuilder.New() | ||
.AddQueryType<QueryWithPagingAttribute>() | ||
.Create(); | ||
|
||
IQueryExecutor executor = schema.MakeExecutable(); | ||
|
||
string query = @" | ||
{ | ||
collection { | ||
edges { | ||
cursor | ||
node | ||
} | ||
pageInfo | ||
{ | ||
hasNextPage | ||
} | ||
totalCount | ||
} | ||
} | ||
"; | ||
|
||
// act | ||
IExecutionResult result = await executor.ExecuteAsync(query); | ||
|
||
// assert | ||
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task UsePagingAttribute_InMemory_Queryable() | ||
{ | ||
// arrange | ||
ISchema schema = SchemaBuilder.New() | ||
.AddQueryType<QueryWithPagingAttribute>() | ||
.Create(); | ||
|
||
IQueryExecutor executor = schema.MakeExecutable(); | ||
|
||
string query = @" | ||
{ | ||
queryable { | ||
edges { | ||
cursor | ||
node | ||
} | ||
pageInfo | ||
{ | ||
hasNextPage | ||
} | ||
totalCount | ||
} | ||
} | ||
"; | ||
|
||
// act | ||
IExecutionResult result = await executor.ExecuteAsync(query); | ||
|
||
// assert | ||
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task UsePagingAttribute_InMemory_Enumerable() | ||
{ | ||
// arrange | ||
ISchema schema = SchemaBuilder.New() | ||
.AddQueryType<QueryWithPagingAttribute>() | ||
.Create(); | ||
|
||
IQueryExecutor executor = schema.MakeExecutable(); | ||
|
||
string query = @" | ||
{ | ||
enumerable { | ||
edges { | ||
cursor | ||
node | ||
} | ||
pageInfo | ||
{ | ||
hasNextPage | ||
} | ||
totalCount | ||
} | ||
} | ||
"; | ||
|
||
// act | ||
IExecutionResult result = await executor.ExecuteAsync(query); | ||
|
||
// assert | ||
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task ConnectionType_Without_Paging_Middleware() | ||
{ | ||
// arrange | ||
ISchema schema = SchemaBuilder.New() | ||
.AddQueryType<QueryWithPagingAttribute>() | ||
.Create(); | ||
|
||
IQueryExecutor executor = schema.MakeExecutable(); | ||
|
||
string query = @" | ||
{ | ||
connectionOfString { | ||
edges { | ||
cursor | ||
node | ||
} | ||
pageInfo | ||
{ | ||
hasNextPage | ||
} | ||
totalCount | ||
} | ||
} | ||
"; | ||
|
||
// act | ||
IExecutionResult result = await executor.ExecuteAsync(query); | ||
|
||
// assert | ||
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task UsePagingAttribute_With_Injected_ConnectionResolver() | ||
{ | ||
// arrange | ||
ISchema schema = SchemaBuilder.New() | ||
.AddQueryType<QueryWithPagingAttribute>() | ||
.Create(); | ||
|
||
IQueryExecutor executor = schema.MakeExecutable(); | ||
|
||
string query = @" | ||
{ | ||
enumerable { | ||
edges { | ||
cursor | ||
node | ||
} | ||
pageInfo | ||
{ | ||
hasNextPage | ||
} | ||
totalCount | ||
} | ||
}"; | ||
|
||
IServiceProvider services = new ServiceCollection() | ||
.AddSingleton<IConnectionResolver<IEnumerable<string>>, ConnectionOfString>() | ||
.BuildServiceProvider(); | ||
|
||
IReadOnlyQueryRequest request = QueryRequestBuilder.New() | ||
.SetQuery(query) | ||
.SetServices(services) | ||
.Create(); | ||
|
||
// act | ||
IExecutionResult result = await executor.ExecuteAsync(request); | ||
|
||
// assert | ||
result.MatchSnapshot(); | ||
} | ||
|
||
public class QueryType | ||
: ObjectType | ||
{ | ||
|
@@ -217,7 +404,7 @@ public class FooType | |
IObjectTypeDescriptor<Foo> descriptor) | ||
{ | ||
descriptor.Interface<FooInterfaceType>(); | ||
descriptor.Field(t => t.Bar).UsePaging<StringType>(); | ||
descriptor.Field<ICollection<string>>(t => t.Bar).UsePaging<StringType>(); | ||
} | ||
} | ||
|
||
|
@@ -238,5 +425,58 @@ public class Foo | |
public ICollection<string> Bar { get; } = | ||
new List<string> { "a", "b", "c", "d", "e", "f", "g" }; | ||
} | ||
|
||
public class QueryWithPagingAttribute | ||
{ | ||
[UsePaging] | ||
public ICollection<string> Collection { get; } = | ||
new List<string> { "a", "b", "c", "d", "e", "f", "g" }; | ||
|
||
[UsePaging] | ||
public IQueryable<string> Queryable { get; } = | ||
new List<string> { "a", "b", "c", "d", "e", "f", "g" }.AsQueryable(); | ||
|
||
[UsePaging] | ||
public IEnumerable<string> Enumerable { get; } = | ||
new List<string> { "a", "b", "c", "d", "e", "f", "g" }.AsQueryable(); | ||
|
||
[GraphQLType(typeof(ConnectionWithCountType<StringType>))] | ||
public Connection<string> ConnectionOfString( | ||
int? first = null, | ||
int? last = null, | ||
string? after = null, | ||
string? before = null) => | ||
new Connection<string>( | ||
new PageInfo(false, false, "foo", "foo", 1), | ||
new List<Edge<string>> { new Edge<string>("abc", "foo") }); | ||
} | ||
|
||
public class ConnectionOfString : IConnectionResolver<IEnumerable<string>> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
benmccallum
Collaborator
|
||
{ | ||
public ValueTask<IConnection> ResolveAsync( | ||
IMiddlewareContext context, | ||
IEnumerable<string> source, | ||
ConnectionArguments arguments = default, | ||
bool withTotalCount = false, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return new ValueTask<IConnection>(new Connection<string>( | ||
new PageInfo(false, false, "foo", "foo", 1), | ||
new List<Edge<string>> { new Edge<string>("abc", "foo") })); | ||
} | ||
|
||
public ValueTask<IConnection> ResolveAsync( | ||
IMiddlewareContext context, | ||
object source, | ||
ConnectionArguments arguments = default, | ||
bool withTotalCount = false, | ||
CancellationToken cancellationToken = default) => | ||
ResolveAsync( | ||
context, | ||
(IEnumerable<string>)source, | ||
arguments, | ||
withTotalCount, | ||
cancellationToken); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Hey @michaelstaib, how is this
IConnectionResolver<T>
implementation used by a resolver?Just above, lines 443 and 444, I would've expected to see that resolver method returning
ConnectionOfString
, or somehow associating this guy, but can't see that as the case.