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

Batch Query Executor #933

Merged
merged 39 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3d70cfd
Introduced batch executor
michaelstaib Jul 22, 2019
da425ed
Added export directive
michaelstaib Jul 22, 2019
3d826ad
Implemented middleware
michaelstaib Jul 22, 2019
b81f4e3
Merge branch 'master' into batching
michaelstaib Jul 22, 2019
7228aa1
Work in progress
michaelstaib Jul 22, 2019
6ba5218
Merge branch 'batching' of github.com:ChilliCream/hotchocolate into b…
michaelstaib Jul 22, 2019
df28898
ongoing work on the batch executor
michaelstaib Jul 22, 2019
8823079
cleanup
michaelstaib Jul 23, 2019
2c64e84
Added collect variables collector
michaelstaib Jul 23, 2019
f4ee395
Infer variables from query
michaelstaib Jul 23, 2019
f5ee51a
Merge branch 'master' into batching
michaelstaib Jul 23, 2019
399f5c1
Added more batch logic
michaelstaib Jul 24, 2019
ffac020
Added end to end tests
michaelstaib Jul 24, 2019
9335e9a
Integrated batching into middleware
michaelstaib Jul 24, 2019
c4d4639
Integrated batching into post middleware
michaelstaib Jul 24, 2019
ba1e4a9
code cleanup
michaelstaib Jul 24, 2019
5d71075
fixed compile error
michaelstaib Jul 24, 2019
55bbb43
Code cleanup
michaelstaib Jul 24, 2019
640d9dd
fixed sonar issue
michaelstaib Jul 24, 2019
7ce9516
fixed sonar issue
michaelstaib Jul 24, 2019
2301f3f
fixed sonar issue
michaelstaib Jul 24, 2019
0e3e899
Export input objects
michaelstaib Jul 29, 2019
49c58db
Added more tests
michaelstaib Jul 29, 2019
3abbe6d
Added list to single value test
michaelstaib Jul 29, 2019
e8e70e2
Added more server modularization
michaelstaib Jul 29, 2019
8831300
fixed references
michaelstaib Jul 29, 2019
49b4225
fixed references
michaelstaib Jul 29, 2019
e304ca5
fixed classic middleware
michaelstaib Jul 30, 2019
4dfe9e2
Added second batch style
michaelstaib Jul 30, 2019
110d07f
Fixed batching issues
michaelstaib Jul 30, 2019
75f8532
Fixed sonar issues
michaelstaib Jul 30, 2019
d810752
Merge branch 'batching' of github.com:ChilliCream/hotchocolate into b…
michaelstaib Jul 30, 2019
3c66054
added more tests
michaelstaib Jul 30, 2019
5e77200
Added more middleware options tests
michaelstaib Jul 30, 2019
a2cd1e4
removed getfirst
michaelstaib Jul 30, 2019
384446b
Added more batch executor tests
michaelstaib Jul 30, 2019
a70f766
sonar issue
michaelstaib Jul 30, 2019
29eef7b
Added more tests
michaelstaib Jul 30, 2019
8bf5876
Merge branch 'batching' of https://github.com/ChilliCream/hotchocolat…
michaelstaib Jul 30, 2019
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
Prev Previous commit
Next Next commit
Added more tests
  • Loading branch information
michaelstaib committed Jul 29, 2019
commit 49c58db56bb6d763ecf9a53c2a1958a443f5e6fd
188 changes: 188 additions & 0 deletions src/Core/Core.Tests/Execution/Batching/BatchQueryExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate;
using HotChocolate.StarWars;
using HotChocolate.Subscriptions;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter;
using Snapshooter.Xunit;
Expand Down Expand Up @@ -150,5 +152,191 @@ mutation secondReview {
r => r.MatchSnapshot(new SnapshotNameExtension("1")),
r => r.MatchSnapshot(new SnapshotNameExtension("2")));
}

[Fact]
public async Task ExecuteExportLeafList()
{
// arrange
var serviceCollection = new ServiceCollection();

serviceCollection
.AddSingleton<ISchema>(sp => SchemaBuilder.New()
.AddServices(sp)
.AddDirectiveType<ExportDirectiveType>()
.AddQueryType(d => d.Name("Query")
.Field("foo")
.Argument("bar", a => a.Type<ListType<StringType>>())
.Type<ListType<StringType>>()
.Resolver<List<string>>(c =>
{
var list = c.Argument<List<string>>("bar");
if (list == null)
{
return new List<string>
{
"123",
"456"
};
}
else
{
list.Add("789");
return list;
}
}))
.Create())
.AddSingleton<IBatchQueryExecutor, BatchQueryExecutor>();

QueryExecutionBuilder.BuildDefault(serviceCollection);

IServiceProvider services =
serviceCollection.BuildServiceProvider();

var executor = services.GetService<IBatchQueryExecutor>();

// act
var batch = new List<IReadOnlyQueryRequest>
{
QueryRequestBuilder.New()
.SetQuery(
@"{
foo @export(as: ""b"")
}")
.Create(),
QueryRequestBuilder.New()
.SetQuery(
@"{
foo(bar: $b)
}")
.Create()
};

IResponseStream stream =
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
}
}

Assert.Collection(results,
r => r.MatchSnapshot(new SnapshotNameExtension("1")),
r => r.MatchSnapshot(new SnapshotNameExtension("2")));
}

[Fact]
public async Task ExecuteExportObjectList()
{
// arrange
var serviceCollection = new ServiceCollection();

serviceCollection
.AddSingleton<ISchema>(sp => SchemaBuilder.New()
.AddServices(sp)
.AddDirectiveType<ExportDirectiveType>()
.AddDocumentFromString(
@"
type Query {
foo(f: [FooInput]) : [Foo]
}

type Foo {
bar: String!
}

input FooInput {
bar: String!
}
")
.AddResolver("Query", "foo", c =>
{
var list =
c.Argument<List<object>>("f");
if (list == null)
{
return new List<object>
{
new Dictionary<string, object>
{
{ "bar" , "123" }
}
};
}
else
{
list.Add(new Dictionary<string, object>
{
{ "bar" , "456" }
});
return list;
}
})
.Use(next => context =>
{
object o = context.Parent<object>();
if (o is Dictionary<string, object> d
&& d.TryGetValue(
context.ResponseName,
out object v))
{
context.Result = v;
}
return next(context);
})
.Create())
.AddSingleton<IBatchQueryExecutor, BatchQueryExecutor>();

QueryExecutionBuilder.BuildDefault(serviceCollection);

IServiceProvider services =
serviceCollection.BuildServiceProvider();

var executor = services.GetService<IBatchQueryExecutor>();

// act
var batch = new List<IReadOnlyQueryRequest>
{
QueryRequestBuilder.New()
.SetQuery(
@"{
foo @export(as: ""b"")
{
bar
}
}")
.Create(),
QueryRequestBuilder.New()
.SetQuery(
@"{
foo(f: $b)
{
bar
}
}")
.Create()
};

IResponseStream stream =
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
}
}

Assert.Collection(results,
r => r.MatchSnapshot(new SnapshotNameExtension("1")),
r => r.MatchSnapshot(new SnapshotNameExtension("2")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Data": {
"foo": [
"123",
"456"
]
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Data": {
"foo": [
"123",
"456",
"789"
]
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Data": {
"foo": [
{
"bar": "123"
}
]
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Data": {
"foo": [
{
"bar": "123"
},
{
"bar": "456"
}
]
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
3 changes: 3 additions & 0 deletions src/Core/Core/Execution/Utilities/DirectiveContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public IImmutableDictionary<string, object> ScopedContextData
set => _middlewareContext.ScopedContextData = value;
}

public NameString ResponseName =>
_middlewareContext.ResponseName;

public T Argument<T>(NameString name) =>
_middlewareContext.Argument<T>(name);

Expand Down
2 changes: 2 additions & 0 deletions src/Core/Core/Execution/Utilities/ResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ internal partial class ResolverContext

public FieldNode FieldSelection => _fieldSelection.Selection;

NameString IResolverContext.ResponseName => _fieldSelection.ResponseName;

public string ResponseName => _fieldSelection.ResponseName;

public IImmutableStack<object> Source { get; private set; }
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Types/Resolvers/IResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public interface IResolverContext
/// </summary>
FieldNode FieldSelection { get; }

/// <summary>
/// Gets the name that the field will have in the response map.
/// </summary>
/// <value></value>
NameString ResponseName { get; }

/// <summary>
/// Gets the source stack containing all previous resolver results
/// of the current execution path.
Expand Down