Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed stitching with add resolver #5143

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
]
}
}