private void writeErrors(ExecutionErrors errors, JsonWriter writer, JsonSerializer serializer)
        {
            if (errors == null || errors.Count == 0)
            {
                return;
            }

            writer.WritePropertyName("errors");

            writer.WriteStartArray();

            errors.Apply(error =>
            {
                writer.WriteStartObject();

                writer.WritePropertyName("message");
                serializer.Serialize(writer, error.Message);

                if (error.Locations != null)
                {
                    writer.WritePropertyName("locations");
                    serializer.Serialize(writer, error.Locations);
                }

                writer.WriteEndObject();
            });

            writer.WriteEndArray();
        }
        /// <summary>
        /// Builds a <see cref="ExecutionContext"/> instance from the provided values.
        /// </summary>
        protected virtual ExecutionContext BuildExecutionContext(ExecutionOptions options, GraphQLDocument document, GraphQLOperationDefinition operation, Variables variables, Metrics metrics)
        {
            var context = new ExecutionContext
            {
                Document    = document,
                Schema      = options.Schema !,
                RootValue   = options.Root,
                UserContext = options.UserContext,

                Operation         = operation,
                Variables         = variables,
                Errors            = new ExecutionErrors(),
                InputExtensions   = options.Extensions ?? Inputs.Empty,
                OutputExtensions  = new Dictionary <string, object?>(),
                CancellationToken = options.CancellationToken,

                Metrics   = metrics,
                Listeners = options.Listeners,
                ThrowOnUnhandledException  = options.ThrowOnUnhandledException,
                UnhandledExceptionDelegate = options.UnhandledExceptionDelegate,
                MaxParallelExecutionCount  = options.MaxParallelExecutionCount,
                RequestServices            = options.RequestServices,
            };

            context.ExecutionStrategy = SelectExecutionStrategy(context);

            return(context);
        }
Example #3
0
 /// <summary>
 /// Creates an <see cref="ExecutionResult"/> with it's <see cref="ExecutionResult.Data" />
 /// property set to the strongly-typed representation of <paramref name="json"/>.
 /// </summary>
 /// <param name="json">A json representation of the <see cref="ExecutionResult.Data"/> to be set.</param>
 /// <param name="errors">Any errors.</param>
 /// <param name="executed">Indicates if the operation included execution.</param>
 /// <returns>ExecutionResult.</returns>
 public static ExecutionResult ToExecutionResult(this string json, ExecutionErrors errors = null, bool executed = true)
 => new ExecutionResult
 {
     Data     = string.IsNullOrWhiteSpace(json) ? null : json.ToDictionary(),
     Errors   = errors,
     Executed = executed
 };
        /// <summary>
        /// Adds errors from the specified <see cref="ExecutionErrors"/> to <see cref="Errors"/>.
        /// </summary>
        /// <param name="errors">List of execution errors.</param>
        /// <returns>Reference to this.</returns>
        public ExecutionResult AddErrors(ExecutionErrors errors)
        {
            if (errors?.Count > 0)
            {
                Errors ??= new ExecutionErrors(errors.Count);

                foreach (var error in errors.List !)
                {
                    Errors.Add(error);
                }
            }

            return(this);
        }
        private void WriteErrors(ExecutionErrors errors, JsonWriter writer, JsonSerializer serializer, bool exposeExceptions)
        {
            if (errors == null || !errors.Any())
            {
                return;
            }

            writer.WritePropertyName("errors");

            writer.WriteStartArray();

            errors.Apply(error =>
            {
                writer.WriteStartObject();

                writer.WritePropertyName("message");

                // check if return StackTrace, including all inner exceptions
                serializer.Serialize(writer, exposeExceptions ? error.ToString() : error.Message);

                if (error.Locations != null)
                {
                    writer.WritePropertyName("locations");
                    writer.WriteStartArray();
                    error.Locations.Apply(location =>
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("line");
                        serializer.Serialize(writer, location.Line);
                        writer.WritePropertyName("column");
                        serializer.Serialize(writer, location.Column);
                        writer.WriteEndObject();
                    });
                    writer.WriteEndArray();
                }

                if (error.Path != null && error.Path.Any())
                {
                    writer.WritePropertyName("path");
                    serializer.Serialize(writer, error.Path);
                }

                WriteErrorExtensions(error, writer, serializer);

                writer.WriteEndObject();
            });

            writer.WriteEndArray();
        }
        private void writeErrors(ExecutionErrors errors, JsonWriter writer, JsonSerializer serializer, bool exposeExceptions)
        {
            if (errors == null || !errors.Any())
            {
                return;
            }

            writer.WritePropertyName("errors");

            writer.WriteStartArray();

            errors.Apply(error =>
            {
                writer.WriteStartObject();

                writer.WritePropertyName("message");
                if (exposeExceptions)
                {
                    serializer.Serialize(writer, error.ToString()); // return StackTrace (including all inner exceptions)
                }
                else
                {
                    serializer.Serialize(writer, error.Message);
                }

                if (error.Locations != null)
                {
                    writer.WritePropertyName("locations");
                    writer.WriteStartArray();
                    error.Locations.Apply(location =>
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("line");
                        serializer.Serialize(writer, location.Line);
                        writer.WritePropertyName("column");
                        serializer.Serialize(writer, location.Column);
                        writer.WriteEndObject();
                    });
                    writer.WriteEndArray();
                }

                writer.WriteEndObject();
            });

            writer.WriteEndArray();
        }
Example #7
0
        private void WriteErrors(ExecutionErrors errors, JsonWriter writer, JsonSerializer serializer, bool exposeExceptions)
        {
            if (errors == null || !errors.Any())
            {
                return;
            }

            writer.WritePropertyName("errors");

            writer.WriteStartArray();

            errors.Apply(error =>
            {
                writer.WriteStartObject();

                writer.WritePropertyName("message");

                // check if return StackTrace, including all inner exceptions
                serializer.Serialize(writer, exposeExceptions ? error.ToString() : error.Message);

                if (error.Locations != null)
                {
                    writer.WritePropertyName("locations");
                    writer.WriteStartArray();
                    error.Locations.Apply(location =>
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("line");
                        serializer.Serialize(writer, location.Line);
                        writer.WritePropertyName("column");
                        serializer.Serialize(writer, location.Column);
                        writer.WriteEndObject();
                    });
                    writer.WriteEndArray();
                }

                if (error.Path != null && error.Path.Any())
                {
                    writer.WritePropertyName("path");
                    serializer.Serialize(writer, error.Path);
                }

                if (!string.IsNullOrWhiteSpace(error.Code))
                {
                    writer.WritePropertyName("code");
                    serializer.Serialize(writer, error.Code);
                }

                if (error.Data != null && error.Data.Count > 0)
                {
                    writer.WritePropertyName("data");
                    writer.WriteStartObject();
                    error.Data.Apply(entry =>
                    {
                        writer.WritePropertyName(entry.Key);
                        serializer.Serialize(writer, entry.Value);
                    });
                    writer.WriteEndObject();
                }

                writer.WriteEndObject();
            });

            writer.WriteEndArray();
        }
 public ValidationResult()
 {
     Errors = new ExecutionErrors();
 }
 public ExecutionContext()
 {
     Fragments = new Fragments();
     Errors    = new ExecutionErrors();
 }