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

Added ResolveWith overload that takes a Delegate #7360

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -233,6 +233,37 @@ IObjectFieldDescriptor ResolveWith<TResolver>(
/// <returns></returns>
IObjectFieldDescriptor ResolveWith(MemberInfo propertyOrMethod);

/// <summary>
/// Adds a resolver based on a delegate to the field.
/// A resolver is a method that resolves the value for a
/// field. The resolver can access parent object, arguments, services and more through the
/// <see cref="IResolverContext"/>.
/// </summary>
/// <param name="delegate">The resolver of the field</param>
/// <example>
/// Given the following resolvers class
/// <code>
/// <![CDATA[
/// private static class Resolvers
/// {
/// public static ValueTask<string> GetFoo(
/// [Service] IFooService service,
/// CancellationToken cancellationToken) =>
/// service.GetFooAsync(cancellationToken);
/// }
/// ]]>
/// </code>
/// The GetFoo method can be mapped like:
/// <code>
/// <![CDATA[
/// descriptor
/// .Field(x => x.Foo)
/// .ResolveWith(Resolvers.GetFoo);
/// ]]>
/// </code>
/// </example>
IObjectFieldDescriptor ResolveWith(Delegate @delegate);

/// <summary>
/// Adds a subscription resolver to to the field
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ public IObjectFieldDescriptor ResolveWith(MemberInfo propertyOrMethod)
return ResolveWithInternal(propertyOrMethod, propertyOrMethod.DeclaringType);
}

/// <inheritdoc />
public IObjectFieldDescriptor ResolveWith(Delegate @delegate)
{
if (@delegate is null)
{
throw new ArgumentNullException(nameof(@delegate));
}

return ResolveWithInternal(@delegate.Method, resolverType: null);
}

private IObjectFieldDescriptor ResolveWithInternal(
MemberInfo propertyOrMethod,
Type? resolverType)
Expand Down
53 changes: 53 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/Types/ObjectTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,30 @@ public void ResolveWithAsync()
.MatchSnapshot();
}

[Fact]
public void ResolveWithStatic()
{
SchemaBuilder.New()
.AddQueryType<ResolveWithStaticQueryType>()
.Create()
.MakeExecutable()
.Execute("{ foo baz }")
.ToJson()
.MatchSnapshot();
}

[Fact]
public void ResolveWithStaticAsync()
{
SchemaBuilder.New()
.AddQueryType<ResolveWithStaticQueryTypeAsync>()
.Create()
.MakeExecutable()
.Execute("{ foo baz qux }")
.ToJson()
.MatchSnapshot();
}

[Fact]
public void ResolveWith_NonGeneric()
{
Expand Down Expand Up @@ -2316,6 +2340,16 @@ public Task<bool> BarAsync(IResolverContext context)
=> Task.FromResult(context is not null);
}

public static class ResolveWithStaticQueryResolver
{
public static string Bar() => "Bar";

public static Task<string> FooAsync() => Task.FromResult("Foo");

public static Task<bool> BarAsync(IResolverContext context)
=> Task.FromResult(context is not null);
}

public class ResolveWithQueryType : ObjectType<ResolveWithQuery>
{
protected override void Configure(IObjectTypeDescriptor<ResolveWithQuery> descriptor)
Expand All @@ -2341,6 +2375,25 @@ protected override void Configure(IObjectTypeDescriptor<ResolveWithQuery> descri
}
}

public class ResolveWithStaticQueryType : ObjectType<ResolveWithQuery>
{
protected override void Configure(IObjectTypeDescriptor<ResolveWithQuery> descriptor)
{
descriptor.Field(t => t.Foo).ResolveWith(ResolveWithStaticQueryResolver.Bar);
descriptor.Field("baz").ResolveWith(ResolveWithStaticQueryResolver.Bar);
}
}

public class ResolveWithStaticQueryTypeAsync : ObjectType<ResolveWithQuery>
{
protected override void Configure(IObjectTypeDescriptor<ResolveWithQuery> descriptor)
{
descriptor.Field(t => t.Foo).ResolveWith(ResolveWithStaticQueryResolver.FooAsync);
descriptor.Field("baz").ResolveWith(ResolveWithStaticQueryResolver.FooAsync);
descriptor.Field("qux").ResolveWith(ResolveWithStaticQueryResolver.BarAsync);
}
}

public class ResolveWithNonGenericObjectType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"foo": "Bar",
"baz": "Bar"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data": {
"foo": "Foo",
"baz": "Foo",
"qux": true
}
}
Loading