Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Transports.AspNetCore/GraphQLHttpMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class GraphQLHttpMiddleware<TSchema>
{
private const string JsonContentType = "application/json";
private const string GraphQLContentType = "application/graphql";
private const string FormUrlEncodedContentType = "application/x-www-form-urlencoded";

private readonly ILogger _logger;
private readonly RequestDelegate _next;
Expand Down Expand Up @@ -66,8 +67,12 @@ public async Task InvokeAsync(HttpContext context)
case GraphQLContentType:
gqlRequest.Query = await ReadAsStringAsync(httpRequest.Body);
break;
case FormUrlEncodedContentType:
var formCollection = await httpRequest.ReadFormAsync();
ExtractGraphQLRequestFromPostBody(formCollection, gqlRequest);
break;
default:
await WriteBadRequestResponseAsync(context, writer, $"Invalid 'Content-Type' header: non-supported media type. Must be of '{JsonContentType}' or '{GraphQLContentType}'. See: http://graphql.org/learn/serving-over-http/.");
await WriteBadRequestResponseAsync(context, writer, $"Invalid 'Content-Type' header: non-supported media type. Must be of '{JsonContentType}', '{GraphQLContentType}', or '{FormUrlEncodedContentType}'. See: http://graphql.org/learn/serving-over-http/.");
return;
}
}
Expand Down Expand Up @@ -144,5 +149,12 @@ private static void ExtractGraphQLRequestFromQueryString(IQueryCollection qs, Gr
gqlRequest.Variables = qs.TryGetValue(GraphQLRequest.VariablesKey, out var variablesValues) ? JObject.Parse(variablesValues[0]) : null;
gqlRequest.OperationName = qs.TryGetValue(GraphQLRequest.OperationNameKey, out var operationNameValues) ? operationNameValues[0] : null;
}

private static void ExtractGraphQLRequestFromPostBody(IFormCollection fc, GraphQLRequest gqlRequest)
{
gqlRequest.Query = fc.TryGetValue(GraphQLRequest.QueryKey, out var queryValues) ? queryValues[0] : null;
gqlRequest.Variables = fc.TryGetValue(GraphQLRequest.VariablesKey, out var variablesValue) ? JObject.Parse(variablesValue[0]) : null;
gqlRequest.OperationName = fc.TryGetValue(GraphQLRequest.OperationNameKey, out var operationNameValues) ? operationNameValues[0] : null;
}
}
}