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 TypeConverter for RelayId #3917

Merged
merged 2 commits into from
Jul 5, 2021
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 @@ -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
}
}