GraphQL Schema-Driven Codegen: Drift Between SDL and Generated Types
0 reputation · 11 Jan 2021, 21:21 UTC
0 reputation · 11 Jan 2021, 21:21 UTC
Maintaining a repeatable development environment for GraphQL often relies on schema-driven code generation. When using a Schema Definition Language (SDL) file or introspection results to generate client-side types, a discrepancy can emerge between the declared schema and the committed artifacts.
In a CI/CD pipeline, the source of truth may shift between a checked-in schema.graphql file, a live introspection query against a development server, or the generated type definitions themselves. This creates uncertainty regarding how to detect drift when server-side changes—such as field deprecations or custom scalar modifications—occur without a corresponding update to the client's generated types.
If generated types are committed to the repository, there is a risk that reviewers may approve changes where the client types no longer align with the server's actual implementation, leading to runtime failures despite successful compilation.
1️⃣ Pin the SDL: Store a committed snapshot of the server schema (e.g., schema.graphql or introspection.json) and reference it in every code‑gen configuration. This guarantees that local generation always uses the same definition the server currently serves.
2️⃣ Diff before generating: In CI, run a schema‑diff tool (e.g., graphql‑inspector diff or graphql‑schema‑diff) against a fresh introspection from the target environment. A non‑zero diff indicates drift and should block the build.
3️⃣ Deterministic regeneration: Execute the code generator with a fixed configuration and the pinned SDL. Capture a checksum (e.g., SHA‑256) of each generated file or of a bundled hash file.
4️⃣ Checksum comparison: Compare the freshly computed checksums with those committed in the repo. Byte‑identical output is verified when the hashes match; otherwise the CI fails.
diff to report changes even when the types are semantically identical.# 1. Fetch live schema
curl -s -X POST -H "Content-Type: application/json" \
-d '{"query":"{ __schema { types { name } } }"}' \
https://api.yourapp.com/graphql \
> live-introspection.json
# 2. Diff pinned vs live
npx graphql-inspector diff schema.graphql live-introspection.json
# Exit 0 = no drift, non‑zero = drift
# 3. Regenerate types deterministically
npx graphql-codegen --config codegen.yml
# 4. Compute checksums
sha256sum src/generated/**/*.ts > types.checksum
# 5. Compare with committed checksum
diff -u types.checksum.orig types.checksum
If your CI targets multiple environments (dev, staging, prod) and you want to ensure the same pinned SDL is used across all, confirm that the same schema.graphql file is checked in and referenced in each environment’s code‑gen config.
Run npx graphql-inspector diff locally against a snapshot of the live introspection you captured from the target environment. If the diff exits cleanly, you can be confident that the SDL and the generated types are in sync before pushing changes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.