Sanity Studio schema deployment and GraphQL API synchronization
0 reputation · 22 Jan 2022, 08:00 UTC
0 reputation · 22 Jan 2022, 08:00 UTC
In Sanity CMS, schemas are defined in code and deployed as part of the Studio bundle. When schema changes occur—such as modifying field types or adding required constraints—these changes immediately affect the Studio UI validation. However, the GraphQL API schema is derived from the Studio schema and requires a separate deployment via sanity graphql deploy to remain synchronized.
A challenge arises when deploying breaking schema changes to a live dataset. Since Sanity does not provide a pre-deployment compatibility check to scan existing documents against a new schema, documents may become invalid upon deployment, surfacing validation errors in the Studio without prior warning.
Given that GraphQL deployments are decoupled from Studio deployments, there is uncertainty regarding the optimal sequence for ensuring zero-downtime API availability during a schema migration.
1. Pipeline strategy – Deploy the Studio bundle and the GraphQL schema from the same commit, and run sanity graphql deploy immediately after a successful Studio deployment. This guarantees that the API schema is always a faithful snapshot of the authoring schema that Studio is currently validating.
2. Pre‑deployment validation – Sanity itself does not ship a built‑in scanner that walks every document and reports incompatibilities before the bundle is live. The usual workaround is to run a custom script (or a dataset query) against the target dataset to surface validation errors before you push the new Studio bundle.
sanity graphql deploy to stay in sync.schemas/ should be version‑controlled and part of the same git commit that will be deployed.sanity deploy --dataset production --project my-project
This updates the Studio bundle and immediately starts validating documents in the production dataset.sanity dataset query production "*[_type=='article']{_id, title, body}" | jq 'select(.body==null)'
If any errors surface, pause the pipeline and backfill or adjust the schema.sanity graphql deploy --dataset production --project my-project
This publishes a GraphQL schema that matches the newly deployed Studio schema.When you must make a field required or change its type:
Because the GraphQL schema can be ahead of Studio (if you deploy GraphQL first) or behind (if you deploy Studio first), clients may experience temporary query failures. The safest approach is:
To tailor the recommendation more precisely, could you confirm whether the planned schema changes are additive (new fields, optional changes) or involve destructive operations (removing or renaming fields, making required)?
sanity.json points to the correct project and dataset.sanity graphql schema preview locally to spot obvious type mismatches before deployment._schema via GraphQL to ensure the new types are present.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.