diff --git a/src/Stitching/Stitching.Tests/Middleware/DelegateToRemoteSchemaMiddlewareTests.cs b/src/Stitching/Stitching.Tests/Middleware/DelegateToRemoteSchemaMiddlewareTests.cs index 0308461cb3b..05a81dc5276 100644 --- a/src/Stitching/Stitching.Tests/Middleware/DelegateToRemoteSchemaMiddlewareTests.cs +++ b/src/Stitching/Stitching.Tests/Middleware/DelegateToRemoteSchemaMiddlewareTests.cs @@ -721,6 +721,96 @@ public async Task StitchedMutation() Snapshot.Match(result); } + [Fact] + public async Task ConnectionLost() + { + // arrange + var connections = new Dictionary(); + IHttpClientFactory clientFactory = CreateRemoteSchemas(connections); + + var serviceCollection = new ServiceCollection(); + serviceCollection.AddSingleton(clientFactory); + serviceCollection.AddStitchedSchema(builder => + builder.AddSchemaFromHttp("contract") + .AddSchemaFromHttp("customer") + .RenameType("CreateCustomerInput", "CreateCustomerInput2") + .AddExtensionsFromString( + FileResource.Open("StitchingExtensions.graphql")) + .AddSchemaConfiguration(c => + c.RegisterType()) + .AddExecutionConfiguration(b => + { + b.AddErrorFilter(error => + { + if (error.Exception is Exception ex) + { + return ErrorBuilder.FromError(error) + .ClearExtensions() + .SetMessage(ex.GetType().FullName) + .SetException(null) + .Build(); + }; + return error; + }); + })); + + IServiceProvider services = + serviceCollection.BuildServiceProvider(); + + IQueryExecutor executor = services + .GetRequiredService(); + IExecutionResult result = null; + + using (IServiceScope scope = services.CreateScope()) + { + var request = new QueryRequest(@" + mutation { + createCustomer(input: { name: ""a"" }) + { + customer { + name + contracts { + id + } + } + } + }"); + request.Services = scope.ServiceProvider; + + result = await executor.ExecuteAsync(request); + } + + var client = new HttpClient + { + BaseAddress = new Uri("http://127.0.0.1") + }; ; + connections["contract"] = client; + connections["customer"] = client; + + // act + using (IServiceScope scope = services.CreateScope()) + { + var request = new QueryRequest(@" + mutation { + createCustomer(input: { name: ""a"" }) + { + customer { + name + contracts { + id + } + } + } + }"); + request.Services = scope.ServiceProvider; + + result = await executor.ExecuteAsync(request); + } + + // assert + Snapshot.Match(result); + } + [Fact] public async Task StitchedMutationWithRenamedInputType() { @@ -1028,6 +1118,12 @@ public async Task } private IHttpClientFactory CreateRemoteSchemas() + { + return CreateRemoteSchemas(new Dictionary()); + } + + private IHttpClientFactory CreateRemoteSchemas( + Dictionary connections) { TestServer server_contracts = TestServerFactory.Create( ContractSchemaFactory.ConfigureSchema, @@ -1039,13 +1135,19 @@ private IHttpClientFactory CreateRemoteSchemas() CustomerSchemaFactory.ConfigureServices, new QueryMiddlewareOptions()); + connections["contract"] = server_contracts.CreateClient(); + connections["customer"] = server_customers.CreateClient(); + var httpClientFactory = new Mock(); httpClientFactory.Setup(t => t.CreateClient(It.IsAny())) .Returns(new Func(n => { - return n.Equals("contract") - ? server_contracts.CreateClient() - : server_customers.CreateClient(); + if (connections.ContainsKey(n)) + { + return connections[n]; + } + + throw new Exception(); })); return httpClientFactory.Object; } diff --git a/src/Stitching/Stitching.Tests/Middleware/__snapshots__/DelegateToRemoteSchemaMiddlewareTests.ConnectionLost.snap b/src/Stitching/Stitching.Tests/Middleware/__snapshots__/DelegateToRemoteSchemaMiddlewareTests.ConnectionLost.snap new file mode 100644 index 00000000000..cafb11df214 --- /dev/null +++ b/src/Stitching/Stitching.Tests/Middleware/__snapshots__/DelegateToRemoteSchemaMiddlewareTests.ConnectionLost.snap @@ -0,0 +1,16 @@ +{ + "Data": { + "createCustomer": null + }, + "Extensions": {}, + "Errors": [ + { + "Message": "System.Net.Http.HttpRequestException", + "Code": null, + "Path": null, + "Locations": [], + "Exception": null, + "Extensions": {} + } + ] +}