Network-layer mocks vs. local schema-mirroring for integration testing
0 reputation · 19 Jul 2020, 21:02 UTC
0 reputation · 19 Jul 2020, 21:02 UTC
Network-layer mocks provide highly deterministic results and fast execution speeds, but they are prone to silent schema drift. If the production schema evolves without the mocks being updated, tests may pass while runtime failures are inevitable due to nullability changes or renamed field types.
Conversely, running a local GraphQL server generated from the latest schema snapshot ensures that resolver contracts and query structures remain valid. However, this approach often fails to replicate production-specific behaviors such as rate limiting logic or specific authorization middleware error shapes. Since introspection is frequently disabled in production environments, relying on static snapshots introduces a risk of stale data.
34775 reputation · 20 Jul 2020, 04:49 UTC
In continuous‑integration pipelines, running a local GraphQL server generated from the latest schema snapshot offers stronger protection against breaking schema changes than static network‑layer mocks. It validates query syntax, field nullability, and resolver contracts against an actual schema, surfacing incompatibilities before code reaches production.
Network‑layer mocks, while fast and deterministic, only validate the shape of the mocked payload. If the production schema evolves—say a field becomes non‑nullable or is renamed—the mock can still return the old shape, causing tests to pass while runtime errors surface in production.
Because introspection is often disabled in production, mock synchronization must rely on an external, versioned schema artifact. A minimal, repeatable process looks like this:
schema.graphql (or JSON introspection result) to a shared artifact store (e.g., Git tag, S3 bucket, or package registry). Include the semantic version in the filename.graphql-codegen --config mocks or a custom script. This guarantees that the mock data model matches the current schema.Network mocks are still useful when you need to emulate production‑specific behaviors that a mirrored server cannot reproduce, such as rate‑limiting, authentication errors, or data‑dependent resolver logic. In those cases, combine the two approaches: use a schema‑mirrored server for structural validation and layer a mock adapter that injects the runtime behaviors you want to test.
Do you currently publish a versioned schema artifact with each release, or is the schema only available in the source repository? The presence or absence of an artifact will influence how tightly you can enforce the synchronization steps above.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.