Cannot query field on type error - Gatsby/Sanity integration issue
Based on the Slack thread context and this being a Gatsby-specific GraphQL error, this is almost certainly a schema synchronization issue between Sanity and Gatsby. Here's how to fix it:
The Root Cause
The "Cannot query field [field] on type [type]" error in Gatsby with gatsby-source-sanity happens when Gatsby's GraphQL schema doesn't match your current Sanity schema. This typically occurs when:
- You've updated your Sanity schema but haven't redeployed the GraphQL API
- Gatsby has cached an old version of your schema
- The GraphQL API deployment failed or is incomplete
Solutions to Try (in order)
1. Redeploy Your Sanity GraphQL API
Every time you update your Sanity schema, you need to redeploy the GraphQL API. From your Sanity Studio folder, run:
sanity graphql deployThis is the most common fix - the GraphQL endpoint doesn't automatically update when you change your schema.
2. Clear Gatsby's Cache
After redeploying, clear Gatsby's cache to force it to fetch the new schema:
gatsby clean
gatsby developThis removes the .cache and public folders and forces Gatsby to rebuild everything from scratch.
3. Verify Your gatsby-config.js
Make sure your gatsby-source-sanity plugin configuration is correct:
{
resolve: 'gatsby-source-sanity',
options: {
projectId: 'your-project-id',
dataset: 'your-dataset',
// If using drafts or private datasets:
token: process.env.SANITY_READ_TOKEN,
watchMode: true, // for development
overlayDrafts: true, // if previewing drafts
}
}4. Check for Field Name Mismatches
The error message should tell you which field is problematic. Verify that:
- The field exists in your Sanity schema
- The field name in your GraphQL query matches exactly (case-sensitive)
- If querying
_rawfields (like_rawBody), these are special Gatsby-specific fields that only work withgatsby-source-sanity, not in Sanity's GraphQL playground
Why This Happens
The gatsby-source-sanity plugin works by fetching GraphQL Schema Definitions from Sanity's deployed GraphQL API. If that deployed schema is out of sync with your actual schema or with what Gatsby has cached, you'll get field query errors.
If you're still stuck after trying these steps, share the specific field name and type from your error message for more targeted troubleshooting!
Show original thread8 replies
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.