From c8db0a6e1af360b56f7d089a6510bb56637e5734 Mon Sep 17 00:00:00 2001 From: Pascal Senn Date: Fri, 10 Jun 2022 13:22:37 +0200 Subject: [PATCH] Fixed stitching with add resolver --- .../Utilities/StitchingTypeInterceptor.cs | 21 ++++-- .../Stitching.Tests/Integration/BaseTests.cs | 73 +++++++++++++++++-- .../BaseTests.LocalField_Execute.snap | 19 +++++ .../BaseTests.Schema_AddResolver.snap | 19 +++++ 4 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.LocalField_Execute.snap create mode 100644 src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.Schema_AddResolver.snap diff --git a/src/HotChocolate/Stitching/src/Stitching/Utilities/StitchingTypeInterceptor.cs b/src/HotChocolate/Stitching/src/Stitching/Utilities/StitchingTypeInterceptor.cs index 4bae757e35d..f9310a77eb2 100644 --- a/src/HotChocolate/Stitching/src/Stitching/Utilities/StitchingTypeInterceptor.cs +++ b/src/HotChocolate/Stitching/src/Stitching/Utilities/StitchingTypeInterceptor.cs @@ -6,6 +6,7 @@ using HotChocolate.Types; using HotChocolate.Types.Descriptors; using HotChocolate.Types.Descriptors.Definitions; +using static HotChocolate.Resolvers.FieldClassMiddlewareFactory; using static HotChocolate.Stitching.WellKnownContextData; namespace HotChocolate.Stitching.Utilities @@ -27,9 +28,9 @@ public override void OnAfterInitialize( if (objectField.GetDirectives().Any(IsDelegatedField)) { FieldMiddleware handleDictionary = - FieldClassMiddlewareFactory.Create(); + Create(); FieldMiddleware delegateToSchema = - FieldClassMiddlewareFactory.Create(); + Create(); objectField.MiddlewareDefinitions.Insert(0, new(handleDictionary)); objectField.MiddlewareDefinitions.Insert(0, new(delegateToSchema)); @@ -62,15 +63,25 @@ public override void OnBeforeCompleteType( { IReadOnlyDictionary> externalFieldLookup = completionContext.GetExternalFieldLookup(); - if (externalFieldLookup.TryGetValue(objectType.Name, out ISet? external)) + if (externalFieldLookup.TryGetValue(objectType.Name, + out ISet? external)) { foreach (ObjectFieldDefinition objectField in objectTypeDef.Fields) { if (external.Contains(objectField.Name) && _handledExternalFields.Add((objectTypeDef.Name, objectField.Name))) { - objectField.Resolvers = new FieldResolverDelegates( - pureResolver: RemoteFieldHelper.RemoteFieldResolver); + if (objectField.Resolvers.HasResolvers) + { + FieldMiddleware handleDictionary = + Create(); + objectField.MiddlewareDefinitions.Insert(0, new(handleDictionary)); + } + else + { + objectField.Resolvers = new FieldResolverDelegates( + pureResolver: RemoteFieldHelper.RemoteFieldResolver); + } } } } diff --git a/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/BaseTests.cs b/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/BaseTests.cs index 71b953ca3be..4f1447cbfb7 100644 --- a/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/BaseTests.cs +++ b/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/BaseTests.cs @@ -72,6 +72,70 @@ public async Task AutoMerge_Execute() result.MatchSnapshot(); } + [Fact] + public async Task LocalField_Execute() + { + // arrange + IHttpClientFactory httpClientFactory = + Context.CreateDefaultRemoteSchemas(); + + IRequestExecutor executor = + await new ServiceCollection() + .AddSingleton(httpClientFactory) + .AddGraphQL() + .AddTypeExtension(new ObjectTypeExtension(d + => d.Name("Query").Field("local").Resolve("I am local."))) + .AddRemoteSchema(Context.ContractSchema) + .AddRemoteSchema(Context.CustomerSchema) + .ModifyRequestOptions(o => o.IncludeExceptionDetails = true) + .BuildRequestExecutorAsync(); + + // act + IExecutionResult result = await executor.ExecuteAsync( + @"{ + local + allCustomers { + id + name + } + }"); + + // assert + result.MatchSnapshot(); + } + + [Fact] + public async Task Schema_AddResolver() + { + // arrange + IHttpClientFactory httpClientFactory = + Context.CreateDefaultRemoteSchemas(); + + IRequestExecutor executor = + await new ServiceCollection() + .AddSingleton(httpClientFactory) + .AddGraphQL() + .AddRemoteSchema(Context.ContractSchema) + .AddRemoteSchema(Context.CustomerSchema) + .AddResolver("Query", "local", "I am local") + .AddTypeExtensionsFromString("extend type Query { local: String }") + .ModifyRequestOptions(o => o.IncludeExceptionDetails = true) + .BuildRequestExecutorAsync(); + + // act + IExecutionResult result = await executor.ExecuteAsync( + @"{ + local + allCustomers { + id + name + } + }"); + + // assert + result.MatchSnapshot(); + } + [Fact] public async Task AutoMerge_Execute_Inline_Fragment() { @@ -195,9 +259,7 @@ public async Task AutoMerge_Execute_Variables() var variables = new Dictionary { - { "customerId", "Q3VzdG9tZXIKZDE=" }, - { "deep", "deep" }, - { "deeper", "deeper" } + { "customerId", "Q3VzdG9tZXIKZDE=" }, { "deep", "deep" }, { "deeper", "deeper" } }; // act @@ -586,10 +648,7 @@ public async Task AutoMerge_Execute_RenameScalar() .ModifyRequestOptions(o => o.IncludeExceptionDetails = true) .BuildRequestExecutorAsync(); - var variables = new Dictionary - { - { "v", new FloatValueNode(1.2f) } - }; + var variables = new Dictionary { { "v", new FloatValueNode(1.2f) } }; // act IExecutionResult result = await executor.ExecuteAsync( diff --git a/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.LocalField_Execute.snap b/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.LocalField_Execute.snap new file mode 100644 index 00000000000..88d1918cd11 --- /dev/null +++ b/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.LocalField_Execute.snap @@ -0,0 +1,19 @@ +{ + "data": { + "local": "I am local.", + "allCustomers": [ + { + "id": "Q3VzdG9tZXIKZDE=", + "name": "Freddy Freeman" + }, + { + "id": "Q3VzdG9tZXIKZDI=", + "name": "Carol Danvers" + }, + { + "id": "Q3VzdG9tZXIKZDM=", + "name": "Walter Lawson" + } + ] + } +} diff --git a/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.Schema_AddResolver.snap b/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.Schema_AddResolver.snap new file mode 100644 index 00000000000..a4a7041a777 --- /dev/null +++ b/src/HotChocolate/Stitching/test/Stitching.Tests/Integration/__snapshots__/BaseTests.Schema_AddResolver.snap @@ -0,0 +1,19 @@ +{ + "data": { + "local": "I am local", + "allCustomers": [ + { + "id": "Q3VzdG9tZXIKZDE=", + "name": "Freddy Freeman" + }, + { + "id": "Q3VzdG9tZXIKZDI=", + "name": "Carol Danvers" + }, + { + "id": "Q3VzdG9tZXIKZDM=", + "name": "Walter Lawson" + } + ] + } +}