Skip to content

Commit aaa3854

Browse files
Fixed ServiceKind.Resolver not working with ResolveWith (#5677) (#5678)
1 parent fb3fe4a commit aaa3854

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

src/HotChocolate/Core/src/Types/Types/Descriptors/ObjectFieldDescriptor.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Reflection;
@@ -20,7 +21,7 @@ public class ObjectFieldDescriptor
2021
, IObjectFieldDescriptor
2122
{
2223
private bool _argumentsInitialized;
23-
private readonly ParameterInfo[] _parameterInfos = Array.Empty<ParameterInfo>();
24+
private ParameterInfo[] _parameterInfos = Array.Empty<ParameterInfo>();
2425

2526
protected ObjectFieldDescriptor(
2627
IDescriptorContext context,
@@ -50,7 +51,9 @@ protected ObjectFieldDescriptor(
5051
MemberKind.ObjectField);
5152
Definition.Type = context.TypeInspector.GetOutputReturnTypeRef(member);
5253
Definition.SourceType = sourceType;
53-
Definition.ResolverType = resolverType == sourceType ? null : resolverType;
54+
Definition.ResolverType = resolverType == sourceType
55+
? null
56+
: resolverType;
5457
Definition.IsParallelExecutable =
5558
context.Options.DefaultResolverStrategy is ExecutionStrategy.Parallel;
5659

@@ -337,6 +340,21 @@ public IObjectFieldDescriptor ResolveWith(
337340
Definition.ResolverMember = propertyOrMethod;
338341
Definition.Resolver = null;
339342
Definition.ResultType = propertyOrMethod.GetReturnType();
343+
344+
if (propertyOrMethod is MethodInfo m)
345+
{
346+
_parameterInfos = m.GetParameters();
347+
348+
var parameters = new Dictionary<NameString, ParameterInfo>();
349+
350+
foreach (ParameterInfo parameterInfo in _parameterInfos)
351+
{
352+
parameters.Add(new NameString(parameterInfo.Name!), parameterInfo);
353+
}
354+
355+
Parameters = parameters;
356+
}
357+
340358
return this;
341359
}
342360

src/HotChocolate/Core/src/Types/Types/Descriptors/OutputFieldDescriptorBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ protected override void OnCreateDefinition(TDefinition definition)
3333
{
3434
base.OnCreateDefinition(definition);
3535

36-
foreach (ArgumentDescriptor argument in Arguments)
36+
if (_arguments is not null)
3737
{
38-
Definition.Arguments.Add(argument.CreateDefinition());
38+
foreach (ArgumentDescriptor argument in Arguments)
39+
{
40+
Definition.Arguments.Add(argument.CreateDefinition());
41+
}
3942
}
4043
}
4144

src/HotChocolate/Core/test/Types.Tests/Resolvers/ResolverServiceTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Threading.Tasks;
22
using HotChocolate.Execution;
33
using HotChocolate.Tests;
4+
using HotChocolate.Types;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.ObjectPool;
67
using Snapshooter.Xunit;
@@ -84,6 +85,32 @@ public async Task AddResolverService()
8485
await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
8586
}
8687

88+
[Fact]
89+
public async Task AddResolverService_2()
90+
{
91+
Snapshot.FullName();
92+
93+
var executor =
94+
await new ServiceCollection()
95+
.AddSingleton<SayHelloService>()
96+
.AddGraphQL()
97+
.AddQueryType<QueryType>()
98+
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
99+
.BuildRequestExecutorAsync();
100+
101+
await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
102+
}
103+
104+
public class QueryType : ObjectType
105+
{
106+
protected override void Configure(IObjectTypeDescriptor descriptor)
107+
{
108+
descriptor
109+
.Field("sayHello")
110+
.ResolveWith<QueryResolverService>(r => r.SayHello(default!));
111+
}
112+
}
113+
87114
public class SayHelloService
88115
{
89116
public string SayHello() => "Hello";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"data": {
3+
"sayHello": "Hello"
4+
}
5+
}

0 commit comments

Comments
 (0)