Skip to content

Commit

Permalink
Fixed ServiceKind.Resolver not working with ResolveWith (#5677)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
tobias-tengler and michaelstaib committed Jan 12, 2023
1 parent cc6a62e commit 195d446
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Expand Up @@ -21,7 +21,7 @@ public class ObjectFieldDescriptor
, IObjectFieldDescriptor
{
private bool _argumentsInitialized;
private readonly ParameterInfo[] _parameterInfos = Array.Empty<ParameterInfo>();
private ParameterInfo[] _parameterInfos = Array.Empty<ParameterInfo>();

/// <summary>
/// Creates a new instance of <see cref="ObjectFieldDescriptor"/>
Expand Down Expand Up @@ -52,7 +52,9 @@ public class ObjectFieldDescriptor
Definition.Description = naming.GetMemberDescription(member, MemberKind.ObjectField);
Definition.Type = context.TypeInspector.GetOutputReturnTypeRef(member);
Definition.SourceType = sourceType;
Definition.ResolverType = resolverType == sourceType ? null : resolverType;
Definition.ResolverType = resolverType == sourceType
? null
: resolverType;
Definition.IsParallelExecutable = context.Options.DefaultResolverStrategy is Parallel;

if (naming.IsDeprecated(member, out var reason))
Expand Down Expand Up @@ -352,6 +354,13 @@ public IObjectFieldDescriptor ResolveWith(MemberInfo propertyOrMethod)
Definition.ResolverMember = propertyOrMethod;
Definition.Resolver = null;
Definition.ResultType = propertyOrMethod.GetReturnType();

if (propertyOrMethod is MethodInfo m)
{
_parameterInfos = m.GetParameters();
Parameters = _parameterInfos.ToDictionary(t => t.Name!, StringComparer.Ordinal);
}

return this;
}

Expand Down
Expand Up @@ -35,9 +35,12 @@ protected override void OnCreateDefinition(TDefinition definition)
{
base.OnCreateDefinition(definition);

foreach (var argument in Arguments)
if (_arguments is not null)
{
Definition.Arguments.Add(argument.CreateDefinition());
foreach (var argument in Arguments)
{
Definition.Arguments.Add(argument.CreateDefinition());
}
}
}

Expand Down
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Tests;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ObjectPool;
using Snapshooter.Xunit;
Expand Down Expand Up @@ -84,6 +85,32 @@ public async Task AddResolverService()
await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
}

[Fact]
public async Task AddResolverService_2()
{
Snapshot.FullName();

var executor =
await new ServiceCollection()
.AddSingleton<SayHelloService>()
.AddGraphQL()
.AddQueryType<QueryType>()
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.BuildRequestExecutorAsync();

await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
}

public class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor
.Field("sayHello")
.ResolveWith<QueryResolverService>(r => r.SayHello(default!));
}
}

public class SayHelloService
{
public string SayHello() => "Hello";
Expand Down
@@ -0,0 +1,5 @@
{
"data": {
"sayHello": "Hello"
}
}

0 comments on commit 195d446

Please sign in to comment.