Skip to content

Commit

Permalink
Fixed stitching with add resolver (#5143)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Jun 10, 2022
1 parent 14f09aa commit 421a64e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,9 +28,9 @@ public override void OnAfterInitialize(
if (objectField.GetDirectives().Any(IsDelegatedField))
{
FieldMiddleware handleDictionary =
FieldClassMiddlewareFactory.Create<DictionaryResultMiddleware>();
Create<DictionaryResultMiddleware>();
FieldMiddleware delegateToSchema =
FieldClassMiddlewareFactory.Create<DelegateToRemoteSchemaMiddleware>();
Create<DelegateToRemoteSchemaMiddleware>();

objectField.MiddlewareDefinitions.Insert(0, new(handleDictionary));
objectField.MiddlewareDefinitions.Insert(0, new(delegateToSchema));
Expand Down Expand Up @@ -62,15 +63,25 @@ public override void OnBeforeCompleteType(
{
IReadOnlyDictionary<NameString, ISet<NameString>> externalFieldLookup =
completionContext.GetExternalFieldLookup();
if (externalFieldLookup.TryGetValue(objectType.Name, out ISet<NameString>? external))
if (externalFieldLookup.TryGetValue(objectType.Name,
out ISet<NameString>? 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<DictionaryResultMiddleware>();
objectField.MiddlewareDefinitions.Insert(0, new(handleDictionary));
}
else
{
objectField.Resolvers = new FieldResolverDelegates(
pureResolver: RemoteFieldHelper.RemoteFieldResolver);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -195,9 +259,7 @@ public async Task AutoMerge_Execute_Variables()

var variables = new Dictionary<string, object>
{
{ "customerId", "Q3VzdG9tZXIKZDE=" },
{ "deep", "deep" },
{ "deeper", "deeper" }
{ "customerId", "Q3VzdG9tZXIKZDE=" }, { "deep", "deep" }, { "deeper", "deeper" }
};

// act
Expand Down Expand Up @@ -586,10 +648,7 @@ public async Task AutoMerge_Execute_RenameScalar()
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.BuildRequestExecutorAsync();

var variables = new Dictionary<string, object>
{
{ "v", new FloatValueNode(1.2f) }
};
var variables = new Dictionary<string, object> { { "v", new FloatValueNode(1.2f) } };

// act
IExecutionResult result = await executor.ExecuteAsync(
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}

0 comments on commit 421a64e

Please sign in to comment.