CannotQuery Field [Field] on type [Type]

3 replies
Last updated: Dec 6, 2021
Consistently getting a "Cannot query field [field] on type [Type]." when I add new fields to an object. Can't see anything which changed before this that could cause it. Any ideas?
AI Update

This is a classic GraphQL schema deployment issue! When you add new fields to your Sanity schema, the GraphQL API doesn't automatically know about them - you need to redeploy your GraphQL schema.

The error "Cannot query field [field] on type [Type]" happens because your GraphQL API generates its schema based on your Sanity content models, but it only updates when you explicitly deploy it. So even though your new fields exist in your Sanity schema and Studio, the GraphQL endpoint is still using the old schema definition.

To fix this, run:

sanity graphql deploy

This command regenerates and deploys your GraphQL schema based on your current Sanity schema, making all your new fields queryable.

Why this happens:

Unlike GROQ (Sanity's native query language), which dynamically queries whatever exists in your documents, GraphQL requires a predefined schema that must be explicitly deployed. This gives you benefits like type safety and schema introspection, but means you need to redeploy whenever your content model changes.

Pro tip: Make it a habit to run sanity graphql deploy after any schema changes if you're using GraphQL. Some teams even add this to their deployment pipeline to ensure the GraphQL schema stays in sync with their content model.

If you're running into this frequently and want more flexibility, you might also consider using GROQ instead, which doesn't require this deployment step since it queries the content dynamically. You can even use both in the same project depending on your needs!

Did you change the name of the sanity dataset? I had the same error but on dev it worked as it pulled the correct one, but build was still using the the “old” dataset.
Also I sometimes forget to CTRL+C when changing the graphql.
As for the fields you also need to “sanity graphql deploy” each time you add new objects or documents to the schema.
Noooo of course this is it! So yes, I was changing between multiple datasets and not deploying the dataset I was consuming! Such a dumb mistake, but thanks for bringing up datasets.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?