Allow callers to skip JSON.generate on the response body#1153
Draft
Allow callers to skip JSON.generate on the response body#1153
Conversation
Adds an optional `body_serializer` callable to `HTTPEndpoint#process` and `Rack::GraphQLEndpoint`. When provided, it receives the GraphQL `result.to_h` hash and returns the response body — a `String`, or any object responding to `each` per the Rack body spec — so a host can write the result hash directly into the response output stream rather than going through the default `JSON.generate(result.to_h)` allocation. Default behavior is unchanged: without a serializer, the body is still a JSON `String` produced by `HTTPResponse.json`. Errors and non-200 responses still produce a `String` body. The Rack adapter wraps `String` bodies into the canonical one-element array and passes any `each`-responding body straight through. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds an optional
body_serializercallable toHTTPEndpoint#processandRack::GraphQLEndpoint. When provided, it receives the GraphQLresult.to_hhash and returns the response body — aString, or any object responding toeachper the Rack body spec — so a host can write the result hash directly into the response output stream rather than going through the defaultJSON.generate(result.to_h)allocation.Motivation
For 200 responses today,
HTTPEndpoint#processalways builds the response body via:That allocates one large JSON
Stringper request. Hosts that have a streaming JSON encoder and a writable response output stream available (for example, a Rack 3 host backed by a server that supports chunked writes) can skip that allocation entirely and emit the result hash straight onto the wire. This change exposes a single hook to do that, without changing any default behavior.Approach
HTTPEndpoint#processnow accepts an optional block. If given, the block is called withresult.to_hand its return value becomesHTTPResponse#body. Without a block, behavior is identical to before —JSON.generate(result.to_h)String body.Rack::GraphQLEndpoint.new(graphql, body_serializer: ...)accepts the same callable and threads it through. The Rack adapter wraps aStringbody in the canonical one-element array (today's behavior) and passes anyeach-responding body straight through to Rack.HTTPResponse#bodyis documented to accept any value the calling adapter accepts. The.jsonand.errorfactories still always produceStringbodies, so error paths and Lambda integrations are unaffected.What's intentionally left out
result.to_hhash before the body callable runs; this PR only changes how that hash is turned into bytes on the wire.@defer/@stream(truly incremental responses) is a separate change.Test plan
script/run_gem_specs elasticgraph-graphql(covers the newHTTPEndpointblock path)script/run_gem_specs elasticgraph-rack(covers the Rack adapter passing a non-String,each-responding body straight through)script/lintscript/type_checkLint and type-check pass locally; the spec runs require a booted test datastore which I'll verify in CI.
🤖 Generated with Claude Code