/// <summary> /// Converts the operation reslt into a dictionary map of the required fields for a graphql response. /// Spec: https://graphql.github.io/graphql-spec/June2018/#sec-Response . /// </summary> /// <param name="writer">The json writer to output the reslts to.</param> /// <param name="resultToWrite">The operation result to write.</param> /// <param name="options">A set options to customize how the response is serialized to the stream.</param> private void WriteResult(Utf8JsonWriter writer, IGraphOperationResult resultToWrite, GraphQLResponseOptions options) { writer.WriteStartObject(); if (resultToWrite.Messages.Severity >= _minSeverityLevel) { writer.WriteStartArray("errors"); foreach (var message in resultToWrite.Messages.Where(x => x.Severity >= _minSeverityLevel)) { this.WriteMessage(writer, message, options); } writer.WriteEndArray(); } if (resultToWrite.Data != null || resultToWrite.Messages.Count == 0) { writer.WritePropertyName("data"); this.WriteObjectCollection(writer, resultToWrite.Data); } if (options.ExposeMetrics && resultToWrite.Metrics != null) { var result = resultToWrite.Metrics.GenerateResult(); writer.WritePropertyName("extensions"); this.WriteObjectCollection(writer, result); } writer.WriteEndObject(); // {document} }
/// <summary> /// Initializes a new instance of the <see cref="GraphQLHttpResponseWriter" /> class. /// </summary> /// <param name="result">The graphql result to serialize.</param> /// <param name="documentWriter">The document writer to perform the serailization.</param> /// <param name="exposeMetrics">if set to <c>true</c> any metrics contained on the result will be exposed and sent to the requestor.</param> /// <param name="exposeExceptions">if set to <c>true</c> exceptions will be writen to the response stream; otherwise false.</param> public GraphQLHttpResponseWriter(IGraphOperationResult result, IGraphQueryResponseWriter documentWriter, bool exposeMetrics, bool exposeExceptions) { _result = result; _documentWriter = documentWriter; _options = new GraphQLResponseOptions() { ExposeExceptions = exposeExceptions, ExposeMetrics = exposeMetrics, }; }
/// <summary> /// Writes the given response directly to the output stream. /// </summary> /// <param name="result">The result.</param> /// <returns>Microsoft.AspNetCore.Mvc.IActionResult.</returns> protected virtual async Task WriteResponse(IGraphOperationResult result) { this.Response.ContentType = Constants.MediaTypes.JSON; if (_schema.Configuration.ResponseOptions.AppendServerHeader) { this.Response.Headers.Add(Constants.ServerInformation.SERVER_INFORMATION_HEADER, Constants.ServerInformation.ServerData); } var localWriter = new GraphQLHttpResponseWriter( result, _writer, this.ExposeMetrics, this.ExposeMetrics); await localWriter.WriteResultAsync(this.HttpContext).ConfigureAwait(false); }
private async Task <string> WriteResponse(IGraphQueryResponseWriter writer, IGraphOperationResult result, GraphQLResponseOptions options = null) { var stream = new MemoryStream(); options = options ?? new GraphQLResponseOptions() { ExposeExceptions = true, ExposeMetrics = true, }; await writer.WriteAsync(stream, result, options); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) return(reader.ReadToEnd()); }
/// <summary> /// Attempts to write the provided <see cref="IGraphOperationResult" /> to the stream. Generally this stream /// will be the response stream for an HTTP request. /// </summary> /// <param name="streamToWriteTo">The stream to write to.</param> /// <param name="resultToWrite">The result to write.</param> /// <param name="options">A set options to customize how the response is serialized to the stream.</param> /// <returns>Task.</returns> public async Task WriteAsync(Stream streamToWriteTo, IGraphOperationResult resultToWrite, GraphQLResponseOptions options = null) { options = options ?? GraphQLResponseOptions.Default; Utf8JsonWriter writer = null; try { writer = new Utf8JsonWriter(streamToWriteTo, _writerOptions); this.WriteResult(writer, resultToWrite, options); } finally { if (writer != null) { await writer.DisposeAsync(); } } }
/// <summary> /// Renders the provided operation result through the response writer and generates a JSON based string output. /// </summary> /// <param name="result">The result of a previously completed operation.</param> /// <returns>Task<System.String>.</returns> private async Task <string> RenderResult(IGraphOperationResult result) { var options = new GraphQLResponseOptions() { ExposeExceptions = this.Schema.Configuration.ResponseOptions.ExposeExceptions, ExposeMetrics = this.Schema.Configuration.ResponseOptions.ExposeMetrics, }; var writer = this.ServiceProvider.GetService <IGraphResponseWriter <TSchema> >(); using (var memStream = new MemoryStream()) { await writer.WriteAsync(memStream, result, options).ConfigureAwait(false); memStream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(memStream)) { return(reader.ReadToEnd()); } } }
/// <summary> /// Finalizes any result being sent to the requester by applying any invocation specific attributes /// to the outgoing object. This is the final processing step before the result is serialized and returned /// to the requester. /// </summary> /// <param name="result">The result generated froma query execution.</param> /// <returns>ExecutionResult.</returns> protected virtual IGraphOperationResult FinalizeResult(IGraphOperationResult result) { return(result); }
/// <summary> /// Attempts to serialize the provided <see cref="IGraphOperationResult" /> directly to the /// provided json writer. /// </summary> /// <param name="jsonWriter">The json writer.</param> /// <param name="resultToWrite">The result to write.</param> /// <param name="options">A set options to customize how the response is serialized to the stream.</param> public void WriteAsync(Utf8JsonWriter jsonWriter, IGraphOperationResult resultToWrite, GraphQLResponseOptions options = null) { this.WriteResult(jsonWriter, resultToWrite, options); }