Skip to content

Commit

Permalink
Wire activity enricher for field errors that are reported at the requ…
Browse files Browse the repository at this point in the history
…est level. (#5745)
  • Loading branch information
michaelstaib committed Jan 31, 2023
1 parent 49f7dc5 commit f08c2ed
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 20 deletions.
Expand Up @@ -193,11 +193,11 @@ public void ResolverError(IMiddlewareContext context, IError error)
}
}

public void ResolverError(IOperation operation, ISelection selection, IError error)
public void ResolverError(IRequestContext context, ISelection selection, IError error)
{
for (var i = 0; i < _listeners.Length; i++)
{
_listeners[i].ResolverError(operation, selection, error);
_listeners[i].ResolverError(context, selection, error);
}
}

Expand Down
Expand Up @@ -98,7 +98,7 @@ public virtual void ResolverError(IMiddlewareContext context, IError error)
}

/// <inheritdoc />
public virtual void ResolverError(IOperation operation, ISelection selection, IError error)
public virtual void ResolverError(IRequestContext context, ISelection selection, IError error)
{
}

Expand Down
Expand Up @@ -213,16 +213,21 @@ public interface IExecutionDiagnosticEvents
/// <summary>
/// Called for field errors that do NOT occur within the resolver task.
/// </summary>
/// <param name="operation">
/// The operation that is being executed.
/// <param name="context">
/// The request context encapsulates all GraphQL-specific information about an
/// individual GraphQL request.
/// </param>
/// <param name="selection">
/// The selection that is affected by the error.
/// </param>
/// <param name="error">
/// The error object.
/// </param>
void ResolverError(IOperation operation, ISelection selection, IError error);
/// <remarks>
/// Some field level errors are handled after the resolver was completed and this
/// are handled in the request scope.
/// </remarks>
void ResolverError(IRequestContext context, ISelection selection, IError error);

/// <summary>
/// Called when starting to run an execution engine task.
Expand Down
Expand Up @@ -56,7 +56,7 @@ public void ResolverError(IMiddlewareContext context, IError error)
{
}

public void ResolverError(IOperation operation, ISelection selection, IError error)
public void ResolverError(IRequestContext context, ISelection selection, IError error)
{
}

Expand Down
Expand Up @@ -20,6 +20,7 @@ internal sealed partial class OperationContext
private readonly DeferredWorkScheduler _deferredWorkScheduler;
private readonly ResultBuilder _resultBuilder;
private readonly PooledPathFactory _pathFactory;
private IRequestContext _requestContext = default!;
private ISchema _schema = default!;
private IErrorHandler _errorHandler = default!;
private IActivator _activator = default!;
Expand Down Expand Up @@ -60,6 +61,7 @@ internal sealed partial class OperationContext
object? rootValue,
Func<object?> resolveQueryRootValue)
{
_requestContext = requestContext;
_schema = requestContext.Schema;
_errorHandler = requestContext.ErrorHandler;
_activator = requestContext.Activator;
Expand All @@ -78,11 +80,12 @@ internal sealed partial class OperationContext
IncludeFlags = _operation.CreateIncludeFlags(variables);
_workScheduler.Initialize(batchDispatcher);
_deferredWorkScheduler.Initialize(this);
_resultBuilder.Initialize(_operation, _errorHandler, _diagnosticEvents);
_resultBuilder.Initialize(_requestContext, _diagnosticEvents);
}

public void InitializeFrom(OperationContext context)
{
_requestContext = context._requestContext;
_schema = context._schema;
_errorHandler = context._errorHandler;
_activator = context._activator;
Expand All @@ -101,7 +104,7 @@ public void InitializeFrom(OperationContext context)
IncludeFlags = _operation.CreateIncludeFlags(_variables);
_workScheduler.Initialize(_batchDispatcher);
_deferredWorkScheduler.InitializeFrom(this, context._deferredWorkScheduler);
_resultBuilder.Initialize(_operation, _errorHandler, _diagnosticEvents);
_resultBuilder.Initialize(_requestContext, _diagnosticEvents);
}

public void Clean()
Expand All @@ -112,6 +115,7 @@ public void Clean()
_workScheduler.Clear();
_resultBuilder.Clear();
_deferredWorkScheduler.Clear();
_requestContext = default!;
_schema = default!;
_errorHandler = default!;
_activator = default!;
Expand Down
Expand Up @@ -25,8 +25,8 @@ internal sealed partial class ResultBuilder
if (!fieldErrors.Contains(violation.Selection))
{
var error = NonNullOutputFieldViolation(path, violation.Selection.SyntaxNode);
error = _errorHandler.Handle(error);
_diagnosticEvents.ResolverError(_operation, violation.Selection, error);
error = _context.ErrorHandler.Handle(error);
_diagnosticEvents.ResolverError(_context, violation.Selection, error);
errors.Add(error);
}

Expand Down
Expand Up @@ -4,8 +4,7 @@ namespace HotChocolate.Execution.Processing;

internal sealed partial class ResultBuilder
{
private IOperation _operation = default!;
private IErrorHandler _errorHandler = default!;
private IRequestContext _context = default!;
private IExecutionDiagnosticEvents _diagnosticEvents = default!;

public ResultBuilder(ResultPool resultPool)
Expand All @@ -15,12 +14,10 @@ public ResultBuilder(ResultPool resultPool)
}

public void Initialize(
IOperation operation,
IErrorHandler errorHandler,
IRequestContext context,
IExecutionDiagnosticEvents diagnosticEvents)
{
_operation = operation;
_errorHandler = errorHandler;
_context = context;
_diagnosticEvents = diagnosticEvents;
}

Expand All @@ -37,8 +34,7 @@ public void Clear()

InitializeResult();

_operation = default!;
_errorHandler = default!;
_context = default!;
_diagnosticEvents = default!;
_data = null;
_items = null;
Expand Down
Expand Up @@ -154,7 +154,7 @@ internal sealed class ListTypeConverter : IChangeTypeProvider
}

var collection = (ICollection<T>)Activator.CreateInstance(listType)!;
ChangeListType(input, (item, _) => collection.Add((T)elementConverter(item)));
ChangeListType(input, (item, _) => collection.Add((T)elementConverter(item)!));
return collection;
}

Expand Down
Expand Up @@ -9,6 +9,7 @@
using GreenDonut;
using HotChocolate.AspNetCore.Instrumentation;
using HotChocolate.Execution;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
using HotChocolate.Language.Utilities;
using HotChocolate.Resolvers;
Expand Down Expand Up @@ -583,6 +584,13 @@ void BuildPath()
Activity activity)
=> EnrichError(error, activity);

public virtual void EnrichResolverError(
IRequestContext context,
ISelection selection,
IError error,
Activity activity)
=> EnrichError(error, activity);

public virtual void EnrichDataLoaderBatch<TKey>(
IDataLoader dataLoader,
IReadOnlyList<TKey> keys,
Expand Down
Expand Up @@ -335,4 +335,18 @@ public override void ResolverError(IMiddlewareContext context, IError error)
activity.SetStatus(ActivityStatusCode.Error);
}
}

public override void ResolverError(IRequestContext context, ISelection selection, IError error)
{
if (!_options.SkipResolveFieldValue &&
context.ContextData.TryGetValue(RequestActivity, out var value))
{
Debug.Assert(value is not null, "The activity mustn't be null!");

var activity = (Activity)value;
_enricher.EnrichResolverError(context, selection, error, activity);
activity.SetStatus(Status.Error);
activity.SetStatus(ActivityStatusCode.Error);
}
}
}

0 comments on commit f08c2ed

Please sign in to comment.