예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentWriter"/> class with the specified settings.
        /// </summary>
        /// <param name="serializerOptions">Specifies the JSON serializer settings</param>
        /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
        public DocumentWriter(JsonSerializerOptions serializerOptions, IErrorInfoProvider errorInfoProvider)
        {
            _options = serializerOptions ?? throw new ArgumentNullException(nameof(serializerOptions));

            // TODO: fix this: it modifies serializerOptions
            ConfigureOptions(errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider)));
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphQLSerializer"/> class with the specified settings.
        /// <br/><br/>
        /// Note that <see cref="JsonSerializerOptions"/> is designed in such a way to reuse created objects.
        /// This leads to a massive speedup in subsequent calls to the serializer. The downside is you can't
        /// change properties on the options object after you've passed it in a
        /// <see cref="JsonSerializer.Serialize(object, Type, JsonSerializerOptions)">Serialize()</see> or
        /// <see cref="JsonSerializer.Deserialize(string, Type, JsonSerializerOptions)">Deserialize()</see> call.
        /// You'll get the exception: <i><see cref="InvalidOperationException"/>: Serializer options cannot be changed
        /// once serialization or deserialization has occurred</i>. To get around this problem we create a copy of
        /// options on NET5 platform and above. Passed options object remains unchanged and any changes you
        /// make are unobserved.
        /// </summary>
        /// <param name="serializerOptions">Specifies the JSON serializer settings</param>
        /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
        public GraphQLSerializer(JsonSerializerOptions serializerOptions, IErrorInfoProvider errorInfoProvider)
        {
#if NET5_0_OR_GREATER
            // clone serializerOptions
            SerializerOptions = new(serializerOptions ?? throw new ArgumentNullException(nameof(serializerOptions)));
#else
            // TODO: fix this: it modifies serializerOptions
            SerializerOptions = serializerOptions ?? throw new ArgumentNullException(nameof(serializerOptions));
#endif

            ConfigureOptions(errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider)));
        }
예제 #3
0
        private void ConfigureOptions(IErrorInfoProvider errorInfoProvider)
        {
            if (!_options.Converters.Any(c => c.CanConvert(typeof(ExecutionResult))))
            {
                _options.Converters.Add(new ExecutionResultJsonConverter(errorInfoProvider ?? new ErrorInfoProvider()));
            }

            if (!_options.Converters.Any(c => c.CanConvert(typeof(JsonConverterBigInteger))))
            {
                _options.Converters.Add(new JsonConverterBigInteger());
            }
        }
예제 #4
0
 /// <summary>
 /// Initializes an instance with the specified <see cref="IErrorInfoProvider"/>.
 /// </summary>
 public GraphQLContractResolver(IErrorInfoProvider errorInfoProvider)
 {
     _errorInfoProvider = errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider));
 }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentWriter"/> class with no indenting and the
 /// specified <see cref="IErrorInfoProvider"/>.
 /// </summary>
 /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
 public DocumentWriter(IErrorInfoProvider errorInfoProvider)
     : this(false, errorInfoProvider)
 {
 }
예제 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentWriter"/> class with the specified settings.
 /// </summary>
 /// <param name="indent">Indicates if child objects should be indented</param>
 /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
 public DocumentWriter(bool indent, IErrorInfoProvider errorInfoProvider)
     : this(GetDefaultSerializerOptions(indent), errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider)))
 {
 }
예제 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentWriter"/> class with the specified settings.
        /// Configuration defaults to no indenting and the specified instance of the <see cref="ErrorInfoProvider"/> class.
        /// </summary>
        /// <param name="configureSerializerOptions">Specifies a callback used to configure the JSON serializer</param>
        /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
        public DocumentWriter(Action <JsonSerializerOptions> configureSerializerOptions, IErrorInfoProvider errorInfoProvider)
        {
            if (configureSerializerOptions == null)
            {
                throw new ArgumentNullException(nameof(configureSerializerOptions));
            }

            if (errorInfoProvider == null)
            {
                throw new ArgumentNullException(nameof(errorInfoProvider));
            }

            _options = GetDefaultSerializerOptions(indent: false);
            configureSerializerOptions.Invoke(_options);

            ConfigureOptions(errorInfoProvider);
        }
        private static void WriteErrors(Utf8JsonWriter writer, ExecutionErrors errors, IErrorInfoProvider errorInfoProvider, JsonSerializerOptions options)
        {
            if (errors == null || errors.Count == 0)
            {
                return;
            }

            writer.WritePropertyName("errors");

            writer.WriteStartArray();

            foreach (var error in errors)
            {
                var info = errorInfoProvider.GetInfo(error);

                writer.WriteStartObject();

                writer.WritePropertyName("message");

                JsonSerializer.Serialize(writer, info.Message, options);

                if (error.Locations != null)
                {
                    writer.WritePropertyName("locations");
                    writer.WriteStartArray();
                    foreach (var location in error.Locations)
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("line");
                        JsonSerializer.Serialize(writer, location.Line, options);
                        writer.WritePropertyName("column");
                        JsonSerializer.Serialize(writer, location.Column, options);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                }

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

                if (info.Extensions?.Count > 0)
                {
                    writer.WritePropertyName("extensions");
                    JsonSerializer.Serialize(writer, info.Extensions, options);
                }

                writer.WriteEndObject();
            }

            writer.WriteEndArray();
        }
 /// <summary>
 /// Creates an instance of <see cref="ExecutionResultJsonConverter"/> with the specified <see cref="IErrorInfoProvider"/>.
 /// </summary>
 public ExecutionResultJsonConverter(IErrorInfoProvider errorInfoProvider)
 {
     _errorInfoProvider = errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphQLSerializer"/> class with no indenting and the
 /// specified <see cref="IErrorInfoProvider"/>.
 /// </summary>
 /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
 public GraphQLSerializer(IErrorInfoProvider errorInfoProvider)
     : this(false, errorInfoProvider)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphQLSerializer"/> class with the specified settings.
 /// </summary>
 /// <param name="indent">Indicates if child objects should be indented</param>
 /// <param name="errorInfoProvider">Specifies the <see cref="IErrorInfoProvider"/> instance to use to serialize GraphQL errors</param>
 public GraphQLSerializer(bool indent, IErrorInfoProvider errorInfoProvider)
     : this(BuildSerializer(indent, null, errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider))))
 {
 }
 /// <summary>
 /// Initializes a new instance with the specified <see cref="IErrorInfoProvider"/> and <see cref="NamingStrategy"/>.
 /// </summary>
 public ExecutionResultJsonConverter(IErrorInfoProvider errorInfoProvider, NamingStrategy namingStrategy)
 {
     _errorInfoProvider = errorInfoProvider ?? throw new ArgumentNullException(nameof(errorInfoProvider));
     _namingStrategy    = namingStrategy;
 }
 /// <summary>
 /// Initializes a new instance with the specified <see cref="IErrorInfoProvider"/>.
 /// </summary>
 public ExecutionResultJsonConverter(IErrorInfoProvider errorInfoProvider)
     : this(errorInfoProvider, null)
 {
 }