Skip to content

Allow callers to skip JSON.generate on the response body#1153

Draft
jwils wants to merge 1 commit intomainfrom
joshuaw/streaming-response-body
Draft

Allow callers to skip JSON.generate on the response body#1153
jwils wants to merge 1 commit intomainfrom
joshuaw/streaming-response-body

Conversation

@jwils
Copy link
Copy Markdown
Collaborator

@jwils jwils commented Apr 27, 2026

Summary

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.

Motivation

For 200 responses today, HTTPEndpoint#process always builds the response body via:

HTTPResponse.json(200, result.to_h)
# => HTTPResponse.new(..., ..., ::JSON.generate(result.to_h))

That allocates one large JSON String per 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#process now accepts an optional block. If given, the block is called with result.to_h and its return value becomes HTTPResponse#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 a String body in the canonical one-element array (today's behavior) and passes any each-responding body straight through to Rack.
  • HTTPResponse#body is documented to accept any value the calling adapter accepts. The .json and .error factories still always produce String bodies, so error paths and Lambda integrations are unaffected.

What's intentionally left out

  • No streaming-from-the-datastore. graphql-ruby's interpreter still builds the full result.to_h hash 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.
  • No new dependencies. Picking a streaming JSON encoder is left to the host.

Test plan

  • script/run_gem_specs elasticgraph-graphql (covers the new HTTPEndpoint block path)
  • script/run_gem_specs elasticgraph-rack (covers the Rack adapter passing a non-String, each-responding body straight through)
  • script/lint
  • script/type_check

Lint and type-check pass locally; the spec runs require a booted test datastore which I'll verify in CI.

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant