public async Task DeserializeQueryResultWithErrors()
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", 123 }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                123
            };

            var result = QueryResultBuilder.New();

            var data = new OrderedDictionary();

            data["foo"] = objectList;
            data["bar"] = scalarList;
            data["baz"] = baz;
            result.SetData(data);

            result.AddError(ErrorBuilder.New()
                            .SetMessage("foo")
                            .SetPath(Path.New("root").Append("child"))
                            .AddLocation(new Location(15, 16))
                            .SetExtension("bar", "baz")
                            .Build());

            result.AddError(ErrorBuilder.New()
                            .SetMessage("qux")
                            .SetExtension("bar", "baz")
                            .Build());

            result.AddError(ErrorBuilder.New()
                            .SetMessage("quux")
                            .Build());

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result.Create(), stream);

            byte[] buffer = stream.ToArray();

            var serializedResult = Utf8GraphQLRequestParser.ParseJson(buffer);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(
                    (IReadOnlyDictionary <string, object>)serializedResult);

            // assert
            Snapshot.Match(deserializedResult);
        }
Example #2
0
 public void EndQueryExecute(IQueryContext context)
 {
     using (var stream = new MemoryStream())
     {
         var resultSerializer = new JsonQueryResultSerializer();
         var str = resultSerializer.Serialize(
             (IReadOnlyQueryResult)context.Result);
         _logger.LogInformation(str, null);
     }
 }
        public async Task DeserializeQueryResultWithErrors()
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", 123 }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                123
            };

            var result = new QueryResult();

            result.Data["foo"] = objectList;
            result.Data["bar"] = scalarList;
            result.Data["baz"] = baz;

            result.Errors.Add(ErrorBuilder.New()
                              .SetMessage("foo")
                              .SetPath(Path.New("root").Append("child"))
                              .AddLocation(new Location(15, 16))
                              .SetExtension("bar", "baz")
                              .Build());

            result.Errors.Add(ErrorBuilder.New()
                              .SetMessage("qux")
                              .SetExtension("bar", "baz")
                              .Build());

            result.Errors.Add(ErrorBuilder.New()
                              .SetMessage("quux")
                              .Build());

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result, stream);

            byte[] buffer = stream.ToArray();

            string json             = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            var    serializedResult = JsonConvert.DeserializeObject <JObject>(json);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(serializedResult);

            // assert
            Snapshot.Match(deserializedResult);
        }
 public void EndQueryExecute(IQueryContext context)
 {
     using (var stream = new MemoryStream())
     {
         var resultSerializer = new JsonQueryResultSerializer();
         resultSerializer.SerializeAsync(
             (IReadOnlyQueryResult)context.Result,
             stream).AsTask().Wait();
         _logger.Information(
             Encoding.UTF8.GetString(stream.ToArray()));
     }
 }
Example #5
0
        /// <summary>
        /// Creates a new instance of <see cref="DefaultHttpResultSerializer" />.
        /// </summary>
        /// <param name="batchSerialization">
        /// Specifies the output-format for batched queries.
        /// </param>
        /// <param name="deferSerialization">
        /// Specifies the output-format for deferred queries.
        /// </param>
        /// <param name="indented">
        /// Defines whether the underlying <see cref="Utf8JsonWriter"/>
        /// should pretty print the JSON which includes:
        /// indenting nested JSON tokens, adding new lines, and adding
        /// white space between property names and values.
        /// By default, the JSON is written without extra white spaces.
        /// </param>
        public DefaultHttpResultSerializer(
            HttpResultSerialization batchSerialization = HttpResultSerialization.MultiPartChunked,
            HttpResultSerialization deferSerialization = HttpResultSerialization.MultiPartChunked,
            bool indented = false)
        {
            _batchSerialization = batchSerialization;
            _deferSerialization = deferSerialization;

            _jsonSerializer      = new(indented);
            _jsonArraySerializer = new(indented);
            _multiPartSerializer = new(indented);
        }
        public async Task DeserializeQueryResultWithExtensions()
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", 123 }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                123
            };

            var result = QueryResultBuilder.New();

            var data = new OrderedDictionary();

            data["foo"] = objectList;
            data["bar"] = scalarList;
            data["baz"] = baz;
            result.SetData(data);

            var extensionData = new ExtensionData();

            extensionData["foo"] = objectList;
            extensionData["bar"] = scalarList;
            extensionData["baz"] = baz;
            result.SetExtensions(extensionData);

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result.Create(), stream);

            byte[] buffer = stream.ToArray();

            var serializedResult = Utf8GraphQLRequestParser.ParseJson(buffer);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(
                    (IReadOnlyDictionary <string, object>)serializedResult);

            // assert
            deserializedResult.MatchSnapshot(m => m.Ignore(c => c.Field <JObject>("Extensions")));
            deserializedResult.Extensions.OrderBy(t => t.Key)
            .MatchSnapshot(new SnapshotNameExtension("extensions"));
        }
Example #7
0
 public void EndExecuteQuery(IQueryContext context)
 {
     if (context.Result is IReadOnlyQueryResult result)
     {
         using (var stream = new MemoryStream())
         {
             var resultSerializer = new JsonQueryResultSerializer();
             resultSerializer.SerializeAsync(
                 result, stream
                 ).GetAwaiter().GetResult();
             _logger.LogInformation(Encoding.UTF8.GetString(stream.ToArray()));
         }
     }
 }
        public async Task <IActionResult> ExecuteFunctionsQueryAsync(HttpContext Context, CancellationToken StopingToken)
        {
            ResultSerializer = new JsonQueryResultSerializer();

            using (Stream Stream = Context.Request.Body)
            {
                if (Context.Request.ContentType.Equals(MediaTypeNames.Application.Json))
                {
                    GraphQueryRequests =
                        await base.ReadJsonRequestAsync(Stream, StopingToken).ConfigureAwait(false);
                }


                else if (Context.Request.ContentType.Equals("application/graphql"))
                {
                    GraphQueryRequests =
                        await base.ReadGraphQLQueryAsync(Stream, StopingToken).ConfigureAwait(false);
                }

                else
                {
                    return(new BadRequestObjectResult("There was either no Query or the Query was mal-formed"));
                }


                if (GraphQueryRequests.Count > 0)
                {
                    var QueryRequest =
                        QueryRequestBuilder.New()
                        .SetServices(ServiceProvider)
                        .SetQuery(GraphQueryRequests[0].Query)
                        .SetOperation(GraphQueryRequests[0].OperationName)
                        .SetQueryName(GraphQueryRequests[0].QueryName);


                    if (GraphQueryRequests[0].Variables != null && GraphQueryRequests[0].Variables.Count > 0)
                    {
                        QueryRequest.SetVariableValues(GraphQueryRequests[0].Variables);
                    }

                    IExecutionResult Result = await Executor.ExecuteAsync(
                        QueryRequest.Create());

                    await ResultSerializer.SerializeAsync(
                        (IReadOnlyQueryResult)Result, Context.Response.Body, StopingToken);
                }

                return(new EmptyResult());
            }
        }
        public GraphQLFunctions(IQueryExecutor executor, IDocumentCache documentCache,
                                IDocumentHashProvider documentHashProvider, IAzureFunctionsMiddlewareOptions azureFunctionsOptions /*JsonQueryResultSerializer jsonQueryResultSerializer*/)
        {
            Executor              = executor;
            DocumentCache         = documentCache;
            DocumentHashProvider  = documentHashProvider;
            AzureFunctionsOptions = azureFunctionsOptions;

            _jsonQueryResultSerializer = new JsonQueryResultSerializer();

            _requestParser = new RequestHelper(
                DocumentCache,
                DocumentHashProvider,
                AzureFunctionsOptions.MaxRequestSize,
                AzureFunctionsOptions.ParserOptions);
        }
Example #10
0
        public async Task DeserializeQueryResultOnlyErrors()
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", 123 }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                123
            };

            var result = new QueryResult();

            result.Errors.Add(new QueryError(
                                  "foo",
                                  Path.New("root").Append("child"),
                                  new List <Location> {
                new Location(15, 16)
            },
                                  new ErrorProperty("bar", "baz")));
            result.Errors.Add(new QueryError(
                                  "qux",
                                  new ErrorProperty("bar", "baz")));
            result.Errors.Add(new QueryError("quux"));

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result, stream);

            byte[] buffer = stream.ToArray();

            string json             = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            var    serializedResult = JsonConvert.DeserializeObject <JObject>(json);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(serializedResult);

            // assert
            Snapshot.Match(deserializedResult);
        }
Example #11
0
        public void EndQueryExecute(IQueryContext context)
        {
            if (!_logger.IsEnabled(LogLevel.Information))
            {
                return;
            }

            using (var stream = new MemoryStream())
            {
                var resultSerializer = new JsonQueryResultSerializer();
                resultSerializer.SerializeAsync(
                    (IReadOnlyQueryResult)context.Result,
                    stream).GetAwaiter().GetResult();
                _logger.LogInformation(
                    Encoding.UTF8.GetString(stream.ToArray()));
            }
        }
Example #12
0
        public static JObject ExtractData(this IExecutionResult executionResult)
        {
            var result = (ReadOnlyQueryResult)executionResult;

            using var stream = new MemoryStream();

            var resultSerializer = new JsonQueryResultSerializer();

            resultSerializer.SerializeAsync(
                result, stream);
            var jsonString = Encoding.UTF8.GetString(stream.ToArray());

            var jObject = JObject.Parse(jsonString);

            var data = (JObject)jObject.GetValue("data");

            return(data);
        }
Example #13
0
        public void EndQueryExecute(IQueryContext context)
        {
            _logger.LogInformation("------ Start BeginQueryExecute ------");

            if (context.Result is IReadOnlyQueryResult result)
            {
                using (var stream = new MemoryStream())
                {
                    var resultSerializer = new JsonQueryResultSerializer();
                    resultSerializer.SerializeAsync(result, stream).Wait();

                    var streamArray = stream.ToArray();
                    _logger.LogInformation(Encoding.UTF8.GetString(streamArray));
                }
            }

            _logger.LogInformation("------ End BeginQueryExecute ------");
        }
Example #14
0
        public async Task DeserializeQueryResultWithExtensions()
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", 123 }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                123
            };

            var result = new QueryResult();

            result.Data["foo"] = objectList;
            result.Data["bar"] = scalarList;
            result.Data["baz"] = baz;

            result.Extensions["foo"] = objectList;
            result.Extensions["bar"] = scalarList;
            result.Extensions["baz"] = baz;

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result, stream);

            byte[] buffer = stream.ToArray();

            string json             = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            var    serializedResult = JsonConvert.DeserializeObject <JObject>(json);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(serializedResult);

            // assert
            Snapshot.Match(deserializedResult);
        }
        public async Task DeserializeQueryResultWithExtensions()
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", 123 }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                123
            };

            var result = new QueryResult();

            result.Data["foo"] = objectList;
            result.Data["bar"] = scalarList;
            result.Data["baz"] = baz;

            result.Extensions["foo"] = objectList;
            result.Extensions["bar"] = scalarList;
            result.Extensions["baz"] = baz;

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result, stream);

            byte[] buffer = stream.ToArray();

            var serializedResult = Utf8GraphQLRequestParser.ParseJson(buffer);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(
                    (IReadOnlyDictionary <string, object>)serializedResult);

            // assert
            Snapshot.Match(deserializedResult);
        }
        public async Task DeserializeQueryResult(object value, string type)
        {
            // arrange
            var qux = new OrderedDictionary {
                { "quux", value }
            };
            var baz = new OrderedDictionary {
                { "qux", qux }
            };
            var objectList = new List <object> {
                baz
            };
            var scalarList = new List <object> {
                value
            };

            var result = QueryResultBuilder.New();
            var data   = new OrderedDictionary();

            data["foo"] = objectList;
            data["bar"] = scalarList;
            data["baz"] = baz;
            result.SetData(data);

            var stream     = new MemoryStream();
            var serializer = new JsonQueryResultSerializer();
            await serializer.SerializeAsync(result.Create(), stream);

            byte[] buffer = stream.ToArray();

            var serializedResult = Utf8GraphQLRequestParser.ParseJson(buffer);

            // act
            IReadOnlyQueryResult deserializedResult =
                HttpResponseDeserializer.Deserialize(
                    (IReadOnlyDictionary <string, object>)serializedResult);

            // assert
            Snapshot.Match(deserializedResult,
                           "DeserializeQueryResult_" + type);
        }
Example #17
0
 public void EndQueryExecute(IQueryContext context)
 {
     if (!ShouldLog)
     {
         return;
     }
     using (var stream = new MemoryStream())
     {
         var resultSerializer = new JsonQueryResultSerializer();
         resultSerializer.SerializeAsync(
             (IReadOnlyQueryResult)context.Result,
             stream).Wait();
         if (Encoding.UTF8.GetString(stream.ToArray()).Contains("String type is most often used by GraphQL to represent free-form human-readable text"))
         {
             return;
         }
         _logger.LogInformation("\n" +
                                Encoding.UTF8.GetString(stream.ToArray()));
     }
     ShouldLog = false;
 }
 /// <summary>
 /// Creates a new instance of <see cref="MultiPartResponseStreamSerializer" />.
 /// </summary>
 /// <param name="indented">
 /// Defines whether the underlying <see cref="Utf8JsonWriter"/>
 /// should pretty print the JSON which includes:
 /// indenting nested JSON tokens, adding new lines, and adding
 /// white space between property names and values.
 /// By default, the JSON is written without any extra white space.
 /// </param>
 public MultiPartResponseStreamSerializer(bool indented = false)
 {
     _payloadSerializer = new(indented);
 }
Example #19
0
 /// <summary>
 /// Creates a new instance of <see cref="JsonArrayResponseStreamSerializer" />.
 /// </summary>
 /// <param name="indented">
 /// Defines whether the underlying <see cref="Utf8JsonWriter"/>
 /// should pretty print the JSON which includes:
 /// indenting nested JSON tokens, adding new lines, and adding
 /// white space between property names and values.
 /// By default, the JSON is written without any extra white space.
 /// </param>
 public JsonArrayResponseStreamSerializer(bool indented = false)
 {
     _serializer = new(indented);
 }