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 ServiceKind.Resolver not working with ResolveWith #5677

Merged
merged 3 commits into from
Jan 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"sayHello": "Hello"
}
}