private static async Task <object?> UnrollResults(object?data, FinalizerContext finalizerContext) { switch (data) { case IFinalizer finalizer: var result = await finalizer.GetValue(finalizerContext).ConfigureAwait(false); return(result); case IDictionary <string, object?> complex: foreach (var entry in complex.Keys.ToArray()) { finalizerContext.CancellationToken.ThrowIfCancellationRequested(); complex[entry] = await UnrollResults(complex[entry], finalizerContext).ConfigureAwait(false); } break; case IEnumerable <object?> list: var items = list.ToArray(); for (var i = 0; i < items.Length; i++) { items[i] = await UnrollResults(items[i], finalizerContext).ConfigureAwait(false); } return(items); default: break; } return(data); }
public async Task <object?> GetValue(FinalizerContext context) { try { var task = taskFactory(); await task.ConfigureAwait(false); var type = GetTaskType(task.GetType()); if (type != null) { var result = GetResultFrom(task, type); return(await context.UnrollResults(result, context).ConfigureAwait(false)); } return(null); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { var errors = ex.HasGraphQlErrors(out var e) ? e : new[] { new GraphQlError(WellKnownErrorCodes.UnhandledError) }; foreach (var error in errors) { error.Fixup(fieldContext); } throw new InvalidOperationException("Caught error in task", ex).AddGraphQlErrors(errors); } }
public static async Task <ExecutionResult> InvokeResult(this IGraphQlScalarResult resolved, object input, ILogger logger, CancellationToken cancellationToken) { if (resolved.Joins.Any()) { throw new InvalidOperationException("Cannot join at the root level"); } var constructedResult = resolved.ConstructResult(); var finalResult = new ExpressionVisitor[] { new ExpressionSimplifier(), }.Aggregate(constructedResult, (prev, next) => next.VisitAndConvert(prev, nameof(InvokeResult))); var result = InvokeExpression(input, finalResult); var finalizerContext = new FinalizerContext(UnrollResults, logger, cancellationToken); var finalizedResult = await UnrollResults(result, finalizerContext).ConfigureAwait(false); return(new ExecutionResult(false, finalizedResult, finalizerContext.Errors.ToArray())); }
public async Task <object?> GetValue(FinalizerContext context) { try { return(await context.UnrollResults(valueAccessor(), context).ConfigureAwait(false)); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { var errors = ex.HasGraphQlErrors(out var e) ? e : new[] { new GraphQlError(WellKnownErrorCodes.UnhandledError) }; foreach (var error in errors) { error.Fixup(fieldContext); } context.Logger.LogError(new EventId(10000, "FieldResolutionCaught"), ex, WellKnownErrorCodes.GetStaticMessage(WellKnownErrorCodes.UnhandledError, errors[0].Arguments), expression); context.Errors.AddRange(errors); return(null); } }