Skip to content

Commit

Permalink
Fixed handling of variables when delegating data fetching through the…
Browse files Browse the repository at this point in the history
… stitching context. (#1390)
  • Loading branch information
Nigel Sampson authored Feb 24, 2020
1 parent 45772fe commit 3f19b6a
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 198 deletions.
17 changes: 15 additions & 2 deletions src/Stitching/Stitching.Tests/Client/RemoteQueryClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
Expand Down Expand Up @@ -285,6 +284,11 @@ public async Task DispatchMultipleQueriesWithVariables()
result.Data["__1__a"] = "b";
result.Data["__1__b"] = "c";

var schema = SchemaBuilder.New()
.AddDocumentFromString("type Query { foo: String }")
.AddResolver("Query", "foo", c => "bar")
.Create();

var executor = new Mock<IQueryExecutor>();
executor.Setup(t => t.ExecuteAsync(
It.IsAny<IReadOnlyQueryRequest>(),
Expand All @@ -296,6 +300,7 @@ public async Task DispatchMultipleQueriesWithVariables()
mergedRequest = r;
return Task.FromResult<IExecutionResult>(result);
}));
executor.Setup(t => t.Schema).Returns(schema);

var request_a = QueryRequestBuilder.New()
.SetQuery("query a($a: String) { a(b: $a) }")
Expand All @@ -319,7 +324,15 @@ public async Task DispatchMultipleQueriesWithVariables()
await task_b;

Assert.Equal(1, count);
mergedRequest.MatchSnapshot();
new
{
Query = mergedRequest.Query.ToString(),
QueryName = mergedRequest.QueryName,
QueryHash = mergedRequest.QueryHash,
OperationName = mergedRequest.OperationName,
Varables = mergedRequest.VariableValues,
Extensions = mergedRequest.Extensions,
}.MatchSnapshot();
}
}
}
108 changes: 108 additions & 0 deletions src/Stitching/Stitching.Tests/Client/StitchingContextTests.cs
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;
}
}
}
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": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Data": {
"foo": 1
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Data": {
"customer": {
"name": "Freddy Freeman"
}
},
"Extensions": {},
"Errors": [],
"ContextData": {}
}
Loading

0 comments on commit 3f19b6a

Please sign in to comment.