Improve projection and serialization performance in router-runtime#2151
Draft
Improve projection and serialization performance in router-runtime#2151
Conversation
…led plan-based stringification Co-authored-by: ardatan <20847995+ardatan@users.noreply.github.com> Agent-Logs-Url: https://github.com/graphql-hive/gateway/sessions/366e876a-4994-4510-bbe1-e628c9a06de1
…n detection Co-authored-by: ardatan <20847995+ardatan@users.noreply.github.com> Agent-Logs-Url: https://github.com/graphql-hive/gateway/sessions/366e876a-4994-4510-bbe1-e628c9a06de1
Copilot created this pull request from a session on behalf of
ardatan
March 23, 2026 23:18
View session
Copilot stopped work on behalf of
ardatan due to an error
March 23, 2026 23:46
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.
GraphQL response projection was building intermediate JS objects on every request by traversing the schema and selection set at runtime. Serialization then repeated the work via
JSON.stringify. This replaces both with a single pre-compiled, plan-based stringification pass.New:
packages/router-runtime/src/stringify/consts.ts— Pre-computed JSON delimiter strings (avoids repeated allocation).data.ts—stringifyWithoutSelectionSet(replacesstableStringify),stringifyString(fast ASCII path that bypassesJSON.stringifyfor plain strings), andprojectWithPlan(buffer-write serializer driven by the compiled plan).error.ts—stringifyErrorwith pre-computed JSON key fragments forGraphQLError.projection-plan.ts— Compiles aProjectionPlanField[]tree once per(schema, OperationDefinitionNode)pair and caches it in aWeakMap. Fields carry pre-escaped JSON key strings, scalar hints, enum value sets,@skip/@includevariable lists, and type guards—so serialization is a straight buffer walk with no schema lookups, nomergeDeep, no intermediate objects.stringify-with-document.ts—stringifyExecutionResult: top-level entry point; retrieves the cached plan and serializesdata + errors + extensionsin one pass.Changes to
executor.tsstableStringify,projectSelectionSet, andprojectDataByOperation.handleRespno longer projects data upfront; returns{ data, errors, stringify }wherestringify(result)performs projection + serialization together.stringifyWithoutSelectionSet.Test updates
polling.test.ts:toEqual→toMatchObject(result now carries thestringifymethod).useOpenTelemetry.spec.ts: HTTP status is always200now that the data field is unconditionally present.