Skip to content

Commit

Permalink
Added TypeConverter for RelayId (#3917)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Jul 5, 2021
1 parent 0ea46fe commit 27af060
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,41 @@ public NodeDescriptor(

private NodeDefinition Definition { get; }

public IObjectFieldDescriptor NodeResolver(
NodeResolverDelegate<TNode, TId> nodeResolver) =>
public IObjectFieldDescriptor NodeResolver(NodeResolverDelegate<TNode, TId> nodeResolver) =>
ResolveNode(nodeResolver);

public IObjectFieldDescriptor ResolveNode(
FieldResolverDelegate fieldResolver)
public IObjectFieldDescriptor ResolveNode(FieldResolverDelegate fieldResolver)
{
Definition.Resolver = fieldResolver ??
throw new ArgumentNullException(nameof(fieldResolver));

return _configureNodeField();
}

public IObjectFieldDescriptor ResolveNode(
NodeResolverDelegate<TNode, TId> fieldResolver) =>
ResolveNode(async ctx =>
public IObjectFieldDescriptor ResolveNode(NodeResolverDelegate<TNode, TId> fieldResolver)
{
ITypeConverter? typeConverter = null;

return ResolveNode(async ctx =>
{
if (ctx.LocalContextData.TryGetValue(
WellKnownContextData.InternalId,
out object? o) && o is TId id)
out object? id))
{
return await fieldResolver(ctx, id).ConfigureAwait(false);
if (id is TId c)
{
return await fieldResolver(ctx, c).ConfigureAwait(false);
}
typeConverter ??= ctx.GetTypeConverter();
c = typeConverter.Convert<object, TId>(id);
return await fieldResolver(ctx, c).ConfigureAwait(false);
}
return null;
});
}

public IObjectFieldDescriptor ResolveNodeWith<TResolver>(
Expression<Func<TResolver, object?>> method)
Expand Down Expand Up @@ -82,8 +91,7 @@ public IObjectFieldDescriptor ResolveNodeWith<TResolver>(
nameof(member));
}

public IObjectFieldDescriptor ResolveNodeWith(
MethodInfo method)
public IObjectFieldDescriptor ResolveNodeWith(MethodInfo method)
{
if (method is null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using HotChocolate.Execution;
using HotChocolate.Data.MongoDb.Filters;
using HotChocolate.Types;
using HotChocolate.Types.Pagination;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson.Serialization.Attributes;
using Snapshooter.Xunit;
using System.Threading.Tasks;
using MongoDB.Bson;
using Xunit;

namespace HotChocolate.Data.MongoDb.Paging
{
public class MongoDbRelayTests
{
[Fact]
public async Task Return_BsonId()
{
Snapshot.FullName();

IRequestExecutor executor = await new ServiceCollection()
.AddTransient<OffsetPagingProvider, MongoDbOffsetPagingProvider>()
.AddGraphQL()
.AddQueryType<Query>()
.AddType<FooType>()
.AddTypeConverter<ObjectId, string>(x => x.ToString())
.BuildRequestExecutorAsync();

IExecutionResult result = await executor
.ExecuteAsync(@"
{
foo {
id
}
}");

result.ToJson().MatchSnapshot();
}

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

IRequestExecutor executor = await new ServiceCollection()
.AddTransient<OffsetPagingProvider, MongoDbOffsetPagingProvider>()
.AddGraphQL()
.AddQueryType<Query>()
.AddType<FooType>()
.AddTypeConverter<ObjectId, string>(x => x.ToString())
.AddTypeConverter<string, ObjectId>(x => ObjectId.Parse(x.ToString()))
.EnableRelaySupport()
.BuildRequestExecutorAsync();

IExecutionResult result = await executor
.ExecuteAsync(@"
{
node(id:""Rm9vCmQ2MGRmMTYyZWQwNzY2ZTE1Y2NlNmIxMGU="") {
id
}
}");

result.ToJson().MatchSnapshot();
}

public class Query
{
public Foo GetFoo() => new Foo { Id = ObjectId.Parse("507f191e810c19729de860ea") };
}

public class FooType : ObjectType<Foo>
{
protected override void Configure(IObjectTypeDescriptor<Foo> descriptor)
{
descriptor.ImplementsNode()
.IdField(x => x.Id)
.ResolveNode((_, objectId) => Task.FromResult(new Foo { Id = objectId }));
}
}

public class Foo
{
[BsonId]
public ObjectId Id { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data": {
"foo": {
"id": "Rm9vCmQ1MDdmMTkxZTgxMGMxOTcyOWRlODYwZWE="
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"node": null
}
}

0 comments on commit 27af060

Please sign in to comment.