/// <summary> /// Executes the result operation of the action method asynchronously. This method is called by MVC to process /// the result of an action method. /// </summary> /// <param name="context">The context in which the result is executed. The context information includes /// information about the action that was executed and request information.</param> /// <returns>A task that represents the asynchronous execute operation.</returns> public async Task WriteResultAsync(HttpContext context) { if (_documentWriter == null) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; if (_options.ExposeExceptions) { await context.Response.WriteAsync(NO_WRITER_WITH_DETAIL).ConfigureAwait(false); } else { await context.Response.WriteAsync(NO_WRITER_NO_DETAIL).ConfigureAwait(false); } } else if (_result == null) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; if (_options.ExposeExceptions) { await context.Response.WriteAsync(NO_RESULT_WITH_DETAIL).ConfigureAwait(false); } else { await context.Response.WriteAsync(NO_RESULT_NO_DETAIL).ConfigureAwait(false); } } else { await _documentWriter.WriteAsync(context.Response.Body, _result, _options).ConfigureAwait(false); } }
/// <summary> /// Writes a specified value as JSON. /// </summary> /// <param name="writer">The writer to write to.</param> /// <param name="value">The value to convert to JSON.</param> /// <param name="options">An object that specifies serialization options to use.</param> public override void Write(Utf8JsonWriter writer, ApolloServerDataMessage value, JsonSerializerOptions options) { writer.WriteStartObject(); writer.WriteString(ApolloConstants.Messaging.MESSAGE_TYPE, ApolloMessageTypeExtensions.Serialize(value.Type)); if (value.Id != null) { writer.WriteString(ApolloConstants.Messaging.MESSAGE_ID, value.Id); } writer.WritePropertyName(ApolloConstants.Messaging.MESSAGE_PAYLOAD); if (value.Payload == null) { writer.WriteNullValue(); } else { _responseWriter.WriteAsync( writer, value.Payload, new GraphQLResponseOptions() { ExposeExceptions = _schema.Configuration.ResponseOptions.ExposeExceptions, ExposeMetrics = _schema.Configuration.ResponseOptions.ExposeMetrics, }); } writer.WriteEndObject(); }
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()); }