How ES6 Proxy-Based GraphQL Query Generation Works
Traditional GraphQL clients such as Apollo Client and Relay require developers to co-locate explicit gql query documents or fragments alongside UI components. Over time, refactored components often leave behind over-fetched fields that silently bloat network payloads. Proxy-based clients invert that workflow: the JavaScript runtime observes which properties your code actually touches and synthesizes the minimal GraphQL document automatically.
1. Two-Pass Selection Collection
When a component renders for the first time against an empty cache, every property access on the root query object invokes a JavaScript Proxy get trap:
- Pass 1 (Collection): Reading
query.user({ id: '7' }).namerecords a selection pathuser(id:"7") { name }and returns a placeholder value (or suspends in Suspense mode). - Batch Flush: At the end of the synchronous render tick, the scheduler merges all recorded paths into a single GraphQL operation and dispatches it via
queryFetcher. - Pass 2 (Resolution): The JSON response hydrates the normalized cache, triggering a single re-render where every property now returns real typed data.
2. Trade-Offs vs Static Query Compilers
| Dimension | Proxy-Based (GQless) | Static Compiler (Relay / Codegen) |
|---|---|---|
| Refactoring Speed | Delete a JSX field → immediately dropped from query | Must manually remove field from fragment & recompile |
| Persisted Queries | Dynamic query strings (requires APQ hashing) | Static query hashes known at build time |
| Conditional Branches | Nested branches may trigger a second fetch unless prefetched | All conditional fields declared upfront in one document |
To get started with the runtime and schema generator, review the CLI Codegen guide and the Core Client API reference.