Issue with Gatsby and Sanity connection, resolved with a workaround.

6 replies
Last updated: May 13, 2022
Well, I’ve been over this and over this researching, implementing and testing and I can’t seem to figure it out, so I’m asking for help. As best as I can figure, something I did disconnected my graphql for Gatsby from my Sanity when I was launching to Netlify. I’m just trying to reconnect them at this point, Before I tried to launch my GraphiQL showed the
allSanityHomepage
kind of data, but now all the Sanity data is not connected. I was able to successfully launch my Sanity to Netlify, so it’s live at a custom domain. My
gatsby-config.js
is set up with the correct projectId, dataset and token - I’ve triple checked and I have used
sanity graphql deploy
(more than once at this point) - the playground does contain all the data correctly too. I think that is all the relevant information. Does anyone have an idea of where I go from here?
AI Update

I feel your frustration - this is actually a common issue that trips people up! The good news is it's usually fixable with a few specific steps. Let me walk you through the most likely causes and solutions.

The Core Issue

When you see your Sanity data in the GraphQL Playground but not in Gatsby's GraphiQL, it means the gatsby-source-sanity plugin isn't successfully fetching your schema or data. Here's what's likely happening:

Most Common Solutions

1. Clear Gatsby's Cache

This is the #1 fix. Gatsby caches GraphQL schemas aggressively, and sometimes doesn't pick up changes:

gatsby clean
gatsby develop

This clears .cache and public folders and forces Gatsby to re-fetch everything from Sanity.

2. Verify Your gatsby-config.js Setup

Double-check your configuration has all required fields:

{
  resolve: 'gatsby-source-sanity',
  options: {
    projectId: 'your-project-id',
    dataset: 'production', // or your dataset name
    token: process.env.SANITY_TOKEN, // if using private dataset
    watchMode: true, // enables hot-reloading in dev
    overlayDrafts: true, // if you want to see drafts
  }
}

3. API Version Parameter

Add an apiVersion to your config - this is often the culprit:

{
  resolve: 'gatsby-source-sanity',
  options: {
    projectId: 'your-project-id',
    dataset: 'production',
    apiVersion: '2023-05-03', // Use today's date in YYYY-MM-DD format
    // ... other options
  }
}

4. Check Your Token Permissions

If you're using a token (required for private datasets or overlayDrafts):

  • Go to manage.sanity.io
  • Navigate to your project → API → Tokens
  • Ensure your token has Viewer or Editor permissions
  • Make sure the token is actually being read (check console.log(process.env.SANITY_TOKEN) temporarily)

5. Verify GraphQL API Deployment

Run this in your Sanity studio folder:

sanity graphql deploy

Then check if the deployment was successful by visiting your GraphQL Playground at https://YOUR-PROJECT-ID.api.sanity.io/v1/graphql/DATASET/default

6. Enable Debug Logging

Set this environment variable to see what gatsby-source-sanity is doing:

SANITY_DEBUG=true gatsby develop

This will show you detailed logs about what the plugin is fetching and any errors it encounters.

Understanding the typePrefix Option

If you're seeing different type names than expected, check if you have a typePrefix option in your config. By default, Gatsby prefixes all Sanity types with "Sanity" (hence allSanityHomepage). If you've changed this, your queries would need to match.

Deployment-Specific Issue

Since you mentioned this happened when launching to Netlify, check:

  • Are your environment variables set in Netlify's dashboard? (Settings → Environment variables)
  • Is your SANITY_TOKEN present in Netlify if you're using overlayDrafts or a private dataset?
  • Try triggering a fresh deploy after clearing Gatsby's cache in your repo

Quick Diagnostic Checklist

  1. ✅ Run gatsby clean && gatsby develop
  2. ✅ Add apiVersion to gatsby-config.js
  3. ✅ Verify token exists and has correct permissions
  4. ✅ Run sanity graphql deploy in studio folder
  5. ✅ Check environment variables in Netlify
  6. ✅ Try SANITY_DEBUG=true gatsby develop to see errors

The fact that your GraphQL Playground works means your Sanity setup is fine - this is purely a Gatsby-side connection issue. Start with the cache clear and apiVersion addition, as those fix 90% of these cases.

Let us know if you're still stuck after trying these steps!

Hey Eric! Maybe it's an issue with your
gatsby-source-sanity
implementation? How do you have it configured?
Thanks for responding, Racheal. Here’s my
gatsby-config.js
for this plugin:
 require('dotenv').config({
  path: `.env`,
});

module.exports = {
  /* Your site config here */
  plugins: [
    {
      resolve: 'gatsby-source-sanity',
      options: {
        projectId: process.env.SANITY_PROJECT_ID,
        dataset: process.env.SANITY_DATASET,
        apiVersion: '2021-09-01',
        watchMode: true,
        token: process.env.SANITY_TOKEN,
      },
    },
  ],
}
I’ve set up a basic environment with only the Gatsby v4 hello world starter,
gatsby-source-sanity
and
gatsby-plugin-sanity-image
and this is what I get when I run
gatsby develop
:
ERROR 

Missing onError handler for invocation 'building-schema', error was 'Error: Type with name "GatsbyImageLayout" does not exists'.
 Stacktrace was 'Error: Type with name "GatsbyImageLayout" does not exists
    at SchemaComposer.get (/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/TypeStorage.ts:39:13)
    at ThunkComposer._thunk (/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/TypeMapper.ts:737:34)
    at ThunkComposer.get ofType [as ofType]
(/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/ThunkComposer.ts:21:34)
    at getTypeNode (/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/definitionNode.ts:253:28)
    at /Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/definitionNode.ts:286:20
    at Array.map (<anonymous>)
    at getArgumentsDefinitionNodes
(/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/definitionNode.ts:284:6)
    at /Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/definitionNode.ts:320:20
    at Array.map (<anonymous>)
    at getFieldDefinitionNodes
(/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/definitionNode.ts:312:6)
    at getObjectTypeDefinitionNode
(/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/definitionNode.ts:61:13)
    at ObjectTypeComposer.getType
(/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/ObjectTypeComposer.ts:986:57)
    at getGraphQLType (/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/utils/typeHelpers.ts:308:17)
    at /Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/SchemaComposer.ts:227:47
    at Array.forEach (<anonymous>)
    at SchemaComposer.buildSchema
(/Users/ericphifer/Desktop/gatsby-prlogin/node_modules/graphql-compose/src/SchemaComposer.ts:226:31)'

⠇ building schema
The building schema never completes
Ok, I figured out the issue with this last one in my test environment with Gatsby v4. I have it running and can access Sanity data through GraphiQL. I’m going to try to migrate my site files and see if I can get this launched to Netlify. I’ll update if I can…
This solution did work - I still don’t know why exactly it broke but it works now. Thank you for your help!
Glad you got it sorted out!

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?