Skip to content

Commit

Permalink
better middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Hidalgo Caballero committed Jan 27, 2018
1 parent a715293 commit 177c91a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/GraphQL.Server.AspNetCore/GraphQLMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private bool IsGraphQLApiRequest(HttpRequest httpRequest) {

private async Task InvokeGraphQLApi(HttpContext httpContext) {
// Read the GraphQLRequest
var request = await this.ReadRequestAsync(httpContext.Request).ConfigureAwait(false);
var request = this.ReadRequest(httpContext.Request);

// Try Execute the request
var result = await this.middlewareSettings.Executer.ExecuteAsync(executionOptions => {
Expand All @@ -70,12 +70,12 @@ private async Task InvokeGraphQLApi(HttpContext httpContext) {
}


private async Task<GraphQLRequest> ReadRequestAsync(HttpRequest httpRequest) {
private GraphQLRequest ReadRequest(HttpRequest httpRequest) {
if (string.Equals(httpRequest.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) {
return this.ReadGetRequest(httpRequest);
}
else if (string.Equals(httpRequest.Method, HttpMethods.Post, StringComparison.OrdinalIgnoreCase)) {
return await this.ReadPostRequestAsync(httpRequest).ConfigureAwait(false);
return this.ReadPostJsonRequest(httpRequest);
}
throw new InvalidOperationException();
}
Expand All @@ -88,10 +88,11 @@ private GraphQLRequest ReadGetRequest(HttpRequest httpRequest) {
};
}

private async Task<GraphQLRequest> ReadPostRequestAsync(HttpRequest httpRequest) {
using (var streamReader = new StreamReader(httpRequest.Body)) {
var body = await streamReader.ReadToEndAsync().ConfigureAwait(true);
return JsonConvert.DeserializeObject<GraphQLRequest>(body);
private GraphQLRequest ReadPostJsonRequest(HttpRequest httpRequest) {
using (var streamReader = new StreamReader(httpRequest.Body))
using (var jsonTextReader = new JsonTextReader(streamReader)) {
var jsonSerializer = new JsonSerializer();
return jsonSerializer.Deserialize<GraphQLRequest>(jsonTextReader);
}
}

Expand Down

0 comments on commit 177c91a

Please sign in to comment.