GraphQL with Gatsby - Update Slug from String to Slug Type Fails

23 replies
Last updated: May 18, 2022
Having issue with Graphql in Gatsby, after I update the slug type from string to slug type. My graphql still fails
Field "slug" must not have a selection since type "String" has no subfields
I have already updated my Sanity Studio data and generate the slugs for it. Am I missing a step? I’m thinking maybe I need to run
sanity deploy
again after publishing the changes ( generating slugs )?** I already run the
sanity graphql deploy
as well
AI Update

I can help you with this! The issue you're experiencing is a common one when changing from a string type to a slug type in Sanity. The problem is that in GraphQL, when a field is a slug type, it becomes an object with subfields (specifically slug.current), not a plain string.

The Core Issue

When you query a slug field in GraphQL, you need to access the current property because Sanity's slug type is an object with this structure:

{
  "_type": "slug",
  "current": "your-slug-value"
}

How to Fix Your Gatsby Queries

You need to update your GraphQL queries in Gatsby. Change from:

{
  allSanityYourType {
    nodes {
      slug  // This won't work anymore
    }
  }
}

To:

{
  allSanityYourType {
    nodes {
      slug {
        current  // Access the current property
      }
    }
  }
}

About sanity graphql deploy

Yes, running sanity graphql deploy again is definitely necessary! This is a crucial step that many developers miss. According to the Sanity documentation, every time you update your Sanity schema (not just the content), you need to re-deploy the GraphQL API for the changes to be reflected.

Steps to Resolve:

  1. Run sanity graphql deploy from your studio folder to update the GraphQL schema
  2. Update all your Gatsby GraphQL queries to access slug.current instead of just slug
  3. If you're using the slug value in your code, update those references too (e.g., node.slug becomes node.slug.current)
  4. Restart your Gatsby development server

Note About Gatsby Integration

The gatsby-source-sanity plugin works by first fetching the GraphQL Schema Definitions from Sanity's deployed GraphQL API, which is why redeploying is essential. Without redeploying, Gatsby is still working with the old schema definition where slug was a string.

Running sanity deploy (for your Studio) is separate from sanity graphql deploy and won't update your GraphQL schema, so make sure you've run the GraphQL-specific deploy command.

It looks like you might be doing
slug { current }
somewhere, but your field is
string
type, which is why it cannot be queried like this.
yeah that’s what I thought so, but I already updated the fields to slug type so i can use
slug { current }
but somehow it still think it’s string so maybe cache?
Updating the schema does not update your documents. Your documents still have a string has a
slug
field. You’ll need to perform a data migration to convert them to the right shape.
yes, I did that already in the Sanity Study, to have a proper slug.
if I query into the sanity vision it returns correct value
slug {
  _type: ""
  _current: ""
}
That doesn’t seem correct to me?
A slug object should be
{ current: "your-value" }
.
sorry its like this
"slug":{
"_type":"slug"
"current":"python-testing"}
Pretty sure the
_type
key is not a thing on a slug object, but that shouldn’t matter for your query.
Are all your documents okay?
yes, no error I was able to publish them as well ( using sanity studio )
I’m thinking do I need to run
sanity deploy
again after publishing the updated data?
Shouldn’t need to.
Is there a way I can tell what dataset the Sanity Studio currently using?
It should be defined in your sanity.json.
how about in the GUI?
Mmmh. Not easily. You can check the Network tabs for the requests. They contain the dataset name.
Thanks
So, the following does look like the correct data format for your slug:
"slug":{
"_type":"slug"
"current":"python-testing"}
Maybe try running
sanity graphql undeploy
then
sanity graphql deploy
to see if that helps?
will this delete my dataset?
>...sanity graphql undeploy --dataset production
> ? Are you absolutely sure you want to delete the current GraphQL API connected to the "production" dataset?
It won't delete the dataset but it will delete the current GraphQL deployment. However, you're deploying a new one directly afterwards.
Thanks! works now I did what you suggested to
undeploy
then
deploy
the sanity graphql 🙂

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?