fix EntityGraphQLEndpointRouteExtensions.MapGraphQL to support chunked requests sent via PostAsJsonAsync
#461
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using
HttpClient.PostAsJsonAsync()to call the GraphQL endpoint (EntityGraphQLEndpointRouteExtensions.MapGraphQL), the request fails with400 Bad Request. This happens becausePostAsJsonAsync:Transfer-Encoding: chunkedContent-LengthThe current implementation in
EntityGraphQLEndpointRouteExtensions.MapGraphQL<T>()rejects all requests whereContentLength == nullor0, making it incompatible with chunked transfers — despite chunked encoding being fully compliant with HTTP/1.1.This behavior makes the GraphQL endpoint incompatible with a common usage of HTTP client libraries.
Fix
In
MapGraphQL<TQueryType>, we now:Transfer-Encoding: chunkedContent-LengthThis ensures the GraphQL endpoint accepts another valid type of HTTP/1.1 requests.
Test Coverage
A new integration test using the existing
CustomWebApplicationFactoryinfrastructure has been added. It runs the app over a real TCP socket (Kestrel) to confirm that:PostAsJsonAsync()sends a chunked requestNote: the test must use communication over the wire, via
CustomWebApplicationFactory, a Testcontainer, or any other way that's not pure in-memory, like withIClassFixture<WebApplicationFactory<Program>>. Otherwise,Content-Lengthis set. I've also tried to reproduce it with the .NET 10 Preview 4 feature "Use WebApplicationFactory with Kestrel for integration testing" but the test passed similar to the way it passes withIClassFixture<WebApplicationFactory<Program>>, so it wasn't a good way to reproduce it with that method:Result
GraphQL over HTTP specPostAsJsonAsync