public async Task ExecuteRequestAsync(RequestContext context) { try { // validate if (string.IsNullOrWhiteSpace(context.RawRequest.Query)) { throw new GraphQLException("Query may not be empty."); } Events.OnRequestStarting(context); var handler = new RequestHandler(this, context); await handler.ExecuteAsync(); } catch (AbortRequestException) { return; // error already added to response } catch (InvalidInputException inpEx) { context.AddInputError(inpEx); } catch (Exception ex) { context.AddError(ex); } finally { context.Metrics.Duration = AppTime.GetDuration(context.StartTimestamp); if (context.Failed) { Events.OnRequestError(context); } Events.OnRequestCompleted(context); } }
public async Task HandleGraphQLHttpRequestAsync(HttpContext httpContext) { if (httpContext.Request.Path.Value.EndsWith("/schema")) { await HandleSchemaDocRequestAsync(httpContext); return; } var start = AppTime.GetTimestamp(); var gqlHttpReq = await BuildGraphQLHttpRequestAsync(httpContext); var reqCtx = gqlHttpReq.RequestContext; //internal request context try { await Server.ExecuteRequestAsync(gqlHttpReq.RequestContext); } catch (Exception exc) { gqlHttpReq.RequestContext.AddError(exc); } // success, serialize response try { var httpResp = httpContext.Response; httpResp.ContentType = ContentTypeJson; var respJson = SerializeResponse(reqCtx.Response); await httpResp.WriteAsync(respJson, httpContext.RequestAborted); reqCtx.Metrics.HttpRequestDuration = AppTime.GetDuration(start); } catch (Exception ex) { // this ex is at attempt to write response as json; we try to write it as plain text and return something await WriteExceptionsAsTextAsync(httpContext, new[] { ex }); } }