Skip to content
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 @@ -231,6 +231,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 @@ -400,6 +400,15 @@ public IObjectFieldDescriptor ResolveWith(MemberInfo propertyOrMethod)
return ResolveWithInternal(propertyOrMethod, propertyOrMethod.DeclaringType);
}

/// <inheritdoc />
public IObjectFieldDescriptor ResolveWith(Delegate @delegate)
{
ArgumentNullException.ThrowIfNull(@delegate);

var method = @delegate.Method;
return ResolveWithInternal(method, method.IsStatic ? null : method.DeclaringType);
}

private IObjectFieldDescriptor ResolveWithInternal(
MemberInfo propertyOrMethod,
Type? resolverType)
Expand Down
93 changes: 93 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,60 @@ 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 ResolveWithInstanceDelegate()
{
SchemaBuilder.New()
.AddQueryType<ResolveWithInstanceDelegateQueryType>()
.Create()
.MakeExecutable()
.Execute("{ foo baz }")
.ToJson()
.MatchSnapshot();
}

[Fact]
public void ResolveWithLambdaDelegate()
{
Func<string> lambda = () => "Lambda";

SchemaBuilder.New()
.AddQueryType(new ObjectType<ResolveWithQuery>(d =>
{
d.Field(t => t.Foo).ResolveWith(lambda);
d.Field("baz").ResolveWith(lambda);
}))
.Create()
.MakeExecutable()
.Execute("{ foo baz }")
.ToJson()
.MatchSnapshot();
}

[Fact]
public void ResolveWith_NonGeneric()
{
Expand Down Expand Up @@ -2353,6 +2407,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 @@ -2378,6 +2442,35 @@ 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 ResolveWithInstanceDelegateQueryType : ObjectType<ResolveWithQuery>
{
protected override void Configure(IObjectTypeDescriptor<ResolveWithQuery> descriptor)
{
var resolver = new ResolveWithQueryResolver();
descriptor.Field(t => t.Foo).ResolveWith(resolver.FooAsync);
descriptor.Field("baz").ResolveWith(resolver.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": "Foo",
"baz": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"foo": "Lambda",
"baz": "Lambda"
}
}
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