-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed handling of variables when delegating data fetching through the…
… stitching context. (#1390)
- Loading branch information
Nigel Sampson
authored
Feb 24, 2020
1 parent
45772fe
commit 3f19b6a
Showing
6 changed files
with
239 additions
and
198 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
108 changes: 108 additions & 0 deletions
108
src/Stitching/Stitching.Tests/Client/StitchingContextTests.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,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using HotChocolate.AspNetCore; | ||
using HotChocolate.AspNetCore.Tests.Utilities; | ||
using HotChocolate.Execution; | ||
using Moq; | ||
using Snapshooter.Xunit; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Stitching.Client | ||
{ | ||
public class StitchingContextTests : StitchingTestBase | ||
{ | ||
public StitchingContextTests( | ||
TestServerFactory testServerFactory) | ||
: base(testServerFactory) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task String_Variable_Is_Converted_To_String_Literal() | ||
{ | ||
// arrange | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddSingleton(CreateRemoteSchemas()); | ||
serviceCollection.AddStitchedSchema(builder => builder | ||
.AddSchemaFromHttp("contract") | ||
.AddSchemaFromHttp("customer")); | ||
|
||
IServiceProvider services = serviceCollection.BuildServiceProvider(); | ||
|
||
IStitchingContext stitchingContext = services.GetRequiredService<IStitchingContext>(); | ||
IRemoteQueryClient customerQueryClient = stitchingContext.GetRemoteQueryClient("customer"); | ||
|
||
IReadOnlyQueryRequest request = QueryRequestBuilder.New() | ||
.SetQuery("query ($id: ID!) { customer(id: $id) { name } }") | ||
.SetVariableValue("id", "Q3VzdG9tZXIKZDE=") | ||
.Create(); | ||
|
||
Task<IExecutionResult> executeTask = customerQueryClient.ExecuteAsync(request); | ||
await customerQueryClient.DispatchAsync(CancellationToken.None); | ||
|
||
IExecutionResult result = await executeTask; | ||
|
||
result.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task Int_Variable_Is_Converted_To_Int_Literal() | ||
{ | ||
// arrange | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddSingleton(CreateFooServer()); | ||
serviceCollection.AddStitchedSchema(builder => builder | ||
.AddSchemaFromString("foo", "type Query { foo(a: Int!) : Int! }")); | ||
|
||
IServiceProvider services = serviceCollection.BuildServiceProvider(); | ||
|
||
IStitchingContext stitchingContext = services.GetRequiredService<IStitchingContext>(); | ||
IRemoteQueryClient customerQueryClient = stitchingContext.GetRemoteQueryClient("foo"); | ||
|
||
IReadOnlyQueryRequest request = QueryRequestBuilder.New() | ||
.SetQuery("query ($foo: Int!) { foo(a: $foo) }") | ||
.SetVariableValue("foo", 1) | ||
.Create(); | ||
|
||
Task<IExecutionResult> executeTask = customerQueryClient.ExecuteAsync(request); | ||
await customerQueryClient.DispatchAsync(CancellationToken.None); | ||
|
||
IExecutionResult result = await executeTask; | ||
|
||
result.MatchSnapshot(); | ||
} | ||
|
||
private IHttpClientFactory CreateFooServer() | ||
{ | ||
var connections = new Dictionary<string, HttpClient>(); | ||
|
||
TestServer foo = TestServerFactory.Create( | ||
services => services.AddGraphQL( | ||
SchemaBuilder.New() | ||
.AddDocumentFromString("type Query { foo(a: Int!) : Int! }") | ||
.AddResolver("Query", "foo", ctx => ctx.Argument<int>("a"))), | ||
app => app.UseGraphQL()); | ||
|
||
connections["foo"] = foo.CreateClient(); | ||
|
||
var httpClientFactory = new Mock<IHttpClientFactory>(); | ||
httpClientFactory.Setup(t => t.CreateClient(It.IsAny<string>())) | ||
.Returns(new Func<string, HttpClient>(n => | ||
{ | ||
if (connections.ContainsKey(n)) | ||
{ | ||
return connections[n]; | ||
} | ||
|
||
throw new Exception(); | ||
})); | ||
|
||
return httpClientFactory.Object; | ||
} | ||
} | ||
} |
201 changes: 8 additions & 193 deletions
201
...sts/Client/__snapshots__/RemoteQueryClientTests.DispatchMultipleQueriesWithVariables.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 |
---|---|---|
@@ -1,200 +1,15 @@ | ||
{ | ||
"Query": { | ||
"Document": { | ||
"Kind": "Document", | ||
"Location": null, | ||
"Definitions": [ | ||
{ | ||
"Kind": "OperationDefinition", | ||
"Location": null, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": null, | ||
"Value": "exec_batch" | ||
}, | ||
"Operation": "Query", | ||
"VariableDefinitions": [ | ||
{ | ||
"Kind": "VariableDefinition", | ||
"Location": { | ||
"Start": 8, | ||
"End": 19, | ||
"Line": 1, | ||
"Column": 9 | ||
}, | ||
"Variable": { | ||
"Kind": "Variable", | ||
"Location": { | ||
"Start": 8, | ||
"End": 11, | ||
"Line": 1, | ||
"Column": 9 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": null, | ||
"Value": "__0__a" | ||
}, | ||
"Value": "__0__a" | ||
}, | ||
"Type": { | ||
"Kind": "NamedType", | ||
"Location": { | ||
"Start": 12, | ||
"End": 19, | ||
"Line": 1, | ||
"Column": 13 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": { | ||
"Start": 12, | ||
"End": 19, | ||
"Line": 1, | ||
"Column": 13 | ||
}, | ||
"Value": "String" | ||
} | ||
}, | ||
"DefaultValue": null, | ||
"Directives": [] | ||
} | ||
], | ||
"Directives": [], | ||
"SelectionSet": { | ||
"Kind": "SelectionSet", | ||
"Location": null, | ||
"Selections": [ | ||
{ | ||
"Kind": "Field", | ||
"Alias": { | ||
"Kind": "Name", | ||
"Location": null, | ||
"Value": "__0__a" | ||
}, | ||
"Arguments": [ | ||
{ | ||
"Kind": "Argument", | ||
"Location": { | ||
"Start": 24, | ||
"End": 30, | ||
"Line": 1, | ||
"Column": 25 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": { | ||
"Start": 24, | ||
"End": 26, | ||
"Line": 1, | ||
"Column": 25 | ||
}, | ||
"Value": "b" | ||
}, | ||
"Value": { | ||
"Kind": "Variable", | ||
"Location": { | ||
"Start": 27, | ||
"End": 30, | ||
"Line": 1, | ||
"Column": 28 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": null, | ||
"Value": "__0__a" | ||
}, | ||
"Value": "__0__a" | ||
} | ||
} | ||
], | ||
"SelectionSet": null, | ||
"Location": { | ||
"Start": 22, | ||
"End": 32, | ||
"Line": 1, | ||
"Column": 23 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": { | ||
"Start": 22, | ||
"End": 24, | ||
"Line": 1, | ||
"Column": 23 | ||
}, | ||
"Value": "a" | ||
}, | ||
"Directives": [] | ||
}, | ||
{ | ||
"Kind": "Field", | ||
"Alias": { | ||
"Kind": "Name", | ||
"Location": null, | ||
"Value": "__1__a" | ||
}, | ||
"Arguments": [], | ||
"SelectionSet": null, | ||
"Location": { | ||
"Start": 10, | ||
"End": 13, | ||
"Line": 1, | ||
"Column": 11 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": { | ||
"Start": 10, | ||
"End": 13, | ||
"Line": 1, | ||
"Column": 11 | ||
}, | ||
"Value": "a" | ||
}, | ||
"Directives": [] | ||
}, | ||
{ | ||
"Kind": "Field", | ||
"Alias": { | ||
"Kind": "Name", | ||
"Location": null, | ||
"Value": "__1__b" | ||
}, | ||
"Arguments": [], | ||
"SelectionSet": null, | ||
"Location": { | ||
"Start": 12, | ||
"End": 15, | ||
"Line": 1, | ||
"Column": 13 | ||
}, | ||
"Name": { | ||
"Kind": "Name", | ||
"Location": { | ||
"Start": 12, | ||
"End": 15, | ||
"Line": 1, | ||
"Column": 13 | ||
}, | ||
"Value": "b" | ||
}, | ||
"Directives": [] | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"Query": "query exec_batch($__0__a: String) { __0__a: a(b: $__0__a) __1__a: a __1__b: b }", | ||
"QueryName": null, | ||
"QueryHash": null, | ||
"OperationName": "exec_batch", | ||
"VariableValues": { | ||
"__0__a": "foo" | ||
"Varables": { | ||
"__0__a": { | ||
"Kind": "StringValue", | ||
"Location": null, | ||
"Value": "foo", | ||
"Block": false | ||
} | ||
}, | ||
"InitialValue": null, | ||
"Properties": {}, | ||
"Services": {}, | ||
"Extensions": {} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../Client/__snapshots__/StitchingContextTests.Int_Variable_Is_Converted_To_Int_Literal.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,8 @@ | ||
{ | ||
"Data": { | ||
"foo": 1 | ||
}, | ||
"Extensions": {}, | ||
"Errors": [], | ||
"ContextData": {} | ||
} |
10 changes: 10 additions & 0 deletions
10
...t/__snapshots__/StitchingContextTests.String_Variable_Is_Converted_To_String_Literal.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,10 @@ | ||
{ | ||
"Data": { | ||
"customer": { | ||
"name": "Freddy Freeman" | ||
} | ||
}, | ||
"Extensions": {}, | ||
"Errors": [], | ||
"ContextData": {} | ||
} |
Oops, something went wrong.