Skip to content

Commit

Permalink
Fixed node resolver and entities resolver (#5080)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored and michaelstaib committed May 19, 2022
1 parent 6126488 commit 88bc2c6
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static class EntitiesResolver
context.SetLocalState(TypeField, objectType);
context.SetLocalState(DataField, current.Data);

tasks[i] = resolver.Invoke(context).AsTask();
tasks[i] = resolver.Invoke(new ResolverContextProxy(context)).AsTask();
}
else
{
Expand Down Expand Up @@ -83,7 +83,7 @@ internal static class EntitiesResolver

private static void ReportError(IResolverContext context, int item, Exception ex)
{
Path itemPath = context.Path.Append(item);
Path itemPath = PathFactory.Instance.Append(context.Path, item);
context.ReportError(ex, error => error.SetPath(itemPath));
}
}
111 changes: 111 additions & 0 deletions src/HotChocolate/Core/src/Types/Types/Helpers/ResolverContextProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types;

namespace HotChocolate.Execution;

internal sealed class ResolverContextProxy : IResolverContext
{
private readonly IResolverContext _resolverContext;

public ResolverContextProxy(IResolverContext resolverContext)
{
_resolverContext = resolverContext;
ScopedContextData = resolverContext.ScopedContextData;
LocalContextData = resolverContext.LocalContextData;
}

public IDictionary<string, object?> ContextData => _resolverContext.ContextData;

public ISchema Schema => _resolverContext.Schema;

public IObjectType RootType => _resolverContext.RootType;

public IObjectType ObjectType => _resolverContext.ObjectType;

public IServiceProvider Services
{
get => _resolverContext.Services;
set => _resolverContext.Services = value;
}

[Obsolete]
public IObjectField Field => _resolverContext.Field;

public DocumentNode Document => _resolverContext.Document;

public OperationDefinitionNode Operation => _resolverContext.Operation;

[Obsolete]
public FieldNode FieldSelection => _resolverContext.FieldSelection;

public NameString ResponseName => _resolverContext.ResponseName;

public Path Path => _resolverContext.Path;

public bool HasErrors => _resolverContext.HasErrors;

public IFieldSelection Selection => _resolverContext.Selection;

public IVariableValueCollection Variables => _resolverContext.Variables;


public IImmutableDictionary<string, object?> ScopedContextData
{
get;
set;
}

public IImmutableDictionary<string, object?> LocalContextData
{
get;
set;
}

public CancellationToken RequestAborted => _resolverContext.RequestAborted;

[Obsolete("Use ArgumentValue<T>(name) or " +
"ArgumentLiteral<TValueNode>(name) or " +
"ArgumentOptional<T>(name).")]
public T? Argument<T>(NameString name) => _resolverContext.Argument<T>(name);

public object Service(Type service) => _resolverContext.Service(service);

public void ReportError(string errorMessage) => _resolverContext.ReportError(errorMessage);

public void ReportError(IError error) => _resolverContext.ReportError(error);

public void ReportError(Exception exception, Action<IErrorBuilder>? configure = null)
=> _resolverContext.ReportError(exception, configure);

public IReadOnlyList<IFieldSelection> GetSelections(
ObjectType typeContext,
SelectionSetNode? selectionSet = null,
bool allowInternals = false)
=> _resolverContext.GetSelections(typeContext, selectionSet, allowInternals);

public T GetQueryRoot<T>()
=> _resolverContext.GetQueryRoot<T>();

public T Parent<T>() => _resolverContext.Parent<T>();

public T ArgumentValue<T>(NameString name) => _resolverContext.ArgumentValue<T>(name);

public TValueNode ArgumentLiteral<TValueNode>(NameString name)
where TValueNode : IValueNode
=> _resolverContext.ArgumentLiteral<TValueNode>(name);

public Optional<T> ArgumentOptional<T>(NameString name)
=> _resolverContext.ArgumentOptional<T>(name);

public ValueKind ArgumentKind(NameString name) => _resolverContext.ArgumentKind(name);

public T Service<T>() => _resolverContext.Service<T>();

public T Resolver<T>() => _resolverContext.Resolver<T>();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

using System;
using System.Buffers;
using System.Collections.Generic;
Expand All @@ -16,8 +18,6 @@
using static HotChocolate.Properties.TypeResources;
using static HotChocolate.Types.WellKnownContextData;

#nullable enable

namespace HotChocolate.Types.Relay;

internal sealed class NodeFieldTypeInterceptor : TypeInterceptor
Expand Down Expand Up @@ -64,10 +64,7 @@ private static void CreateNodeField(
Node,
Relay_NodeField_Description,
node,
ResolveNodeAsync)
{
Arguments = { new(Id, Relay_NodeField_Id_Description, id) }
};
ResolveNodeAsync) { Arguments = { new(Id, Relay_NodeField_Id_Description, id) } };

fields.Insert(index, field);

Expand All @@ -87,10 +84,7 @@ private static void CreateNodesField(
Nodes,
Relay_NodesField_Description,
nodes,
ResolveNodeAsync)
{
Arguments = { new(Ids, Relay_NodesField_Ids_Description, ids) }
};
ResolveNodeAsync) { Arguments = { new(Ids, Relay_NodesField_Ids_Description, ids) } };

fields.Insert(index, field);

Expand Down Expand Up @@ -153,7 +147,7 @@ private static void CreateNodesField(
context.Schema.TryGetType<ObjectType>(typeName, out ObjectType? type) &&
type.ContextData.TryGetValue(NodeResolver, out var o) &&
o is FieldResolverDelegate resolver
? resolver.Invoke(context).AsTask()
? resolver.Invoke(new ResolverContextProxy(context)).AsTask()
: _nullTask;
}

Expand Down

0 comments on commit 88bc2c6

Please sign in to comment.