New field in Sanity schema not appearing in GraphQL API

12 replies
Last updated: Aug 11, 2020
I added a new field in fields array in siteSettings document and updated it's value with studio. But the field is not available in graphQL API.
{ name: 'siteURL',
      type: 'url',
      title: 'Site URL',
      description: 'Provide URL for your website.'
    },
What could be the issue? I did close the process and rebuild the gatsby site after updating.

EDIT: Solved
AI Update

Great that you solved it! For anyone else running into this issue, here's what's likely happening:

When you add a new field to your Sanity schema, you need to redeploy your GraphQL endpoint by running:

sanity graphql deploy

This is a separate step from deploying your Studio or rebuilding your Gatsby site. The GraphQL API is generated from your schema, so every time you make schema changes (adding/removing fields, changing types, etc.), you need to redeploy it.

Why this matters for Gatsby:

The gatsby-source-sanity plugin works by first fetching the GraphQL Schema Definitions from Sanity's deployed GraphQL API, then streaming all accessible documents to Gatsby's in-memory datastore. Without redeploying the GraphQL API, the plugin won't know about your new field.

Common workflow when adding new fields:

  1. Add the field to your schema (like your siteURL field)
  2. Run sanity graphql deploy from your Studio directory
  3. Update content in Studio
  4. Rebuild your Gatsby site

Pro tip: You can use sanity graphql deploy --dry-run to validate your schema changes before deploying, which will catch any breaking changes without actually deploying.

Note: While gatsby-source-sanity uses the deployed GraphQL API for schema definitions, Sanity also supports querying directly with GROQ if you prefer that approach. But if you're using the standard GraphQL setup with Gatsby, the sanity graphql deploy step is essential after schema changes.

Show original thread
12 replies
Hi Rahul, did you deploy your GraphQL API after adding the new field? You can do so by running
sanity graphql deploy
in the CLI 🙂
user M
It throws an error

- Checking for deployed API

Error: Request returned HTTP 401

at D:/WebDev Projects/Gatsby/The Gatsby Blog/Sanity Blog Studio/node_modules/@sanity/core/lib/util/getUrlHeaders.js:30:21

at f (D:/WebDev Projects/Gatsby/The Gatsby Blog/Sanity Blog Studio/node_modules/once/once.js:25:25)

at ClientRequest.<anonymous> (D:/WebDev Projects/Gatsby/The Gatsby Blog/Sanity Blog Studio/node_modules/simple-get/index.js:63:5)
Could you run
sanity logout && sanity login
and then try once again?
401
indicates an authentication issue in this case.
I did, and now it says I need a top-level scheme to deploy.

Error: Encountered anonymous inline code at index 2 for type/field Post/body.

post.js

{ name: 'body',

type: 'bodyPortableText',
 
title: 'Body'

}

bodyportableText.js


_export_ _default_ {
  
name: 'bodyPortableText',
  
type: 'array',
  
title: 'Post body',
  
of: [
    
{
      
type: 'block',
      
title: 'Block',

      _// Styles let you set what your user can mark up blocks with. These_
      
// corrensponds with HTML tags, but you can set any title or value
      
// you want and decide how you want to deal with it where you want to
      
// use your content.
      
styles: [
        
{ title: 'Normal', value: 'normal' },
        
{ title: 'H1', value: 'h1' },
        
{ title: 'H2', value: 'h2' },
        
{ title: 'H3', value: 'h3' },
        
{ title: 'H4', value: 'h4' },
        
{ title: 'Quote', value: 'blockquote' }
      
],
      
lists: [
        
{ title: 'Bullet', value: 'bullet' },
        
{ title: 'Number', value: 'number' }
      
],
      
// Marks let you mark up inline text in the block editor.
      
marks: {
        
// Decorators usually describe a single property – e.g. a typographic
        
// preference or highlighting by editors.
        
decorators: [
          
{ title: 'Strong', value: 'strong' },
          
{ title: 'Emphasis', value: 'em' },
          
{ title: 'Underline', value: 'underline' },
          
{ title: 'Strike', value: 'strike-through' }
        
],
        
// Annotations can be any object structure – e.g. a link or a footnote.
        
annotations: [
          
{
            
name: 'link',
            
type: 'object',
            
title: 'URL',
            
fields: [
              
{
                
title: 'URL',
                
name: 'href',
                
type: 'url'
              
},
              
{
                
title: 'Open externally',
                
name: 'blank',
                
type: 'boolean'
              
}
            
]
          
},
          
{
            
name: 'internalLink',
            
type: 'object',
            
title: 'Internal Link',
            
blockEditor: {
              
_icon: () =>_ ':link:'
            
},
            
fields: [
              
{
                
name: 'link',
                
type: 'string',
                
description: 'Paste only the slug for post eg: "my-first-post"'
              
}
            
]
          
}
        
]
      
},
      
of: [{ type: 'authorReference' }]
    
},

    _// You can add additional types here. Note that you can't use_
    
// primitive types such as 'string' and 'number' in the same array
    
// as a block type.
    
{
      
type: 'mainImage',
      
_options: { hotspot: true_ }
    
},
    
{
      
name: 'myCode',
      
title: 'Code Editor',
      
description: 'Code editor',
      
type: 'code'
    
}
  
]

}
I am using the Sanity blog starter template
user M
Just tested locally to be sure, but that template does indeed deploy a GraphQL API out-of-the-box. Did you perhaps add any new objects to your schema that are declared inline (within another document type) instead of at the top level? Here’s some additional information on the error you are seeing: https://www.sanity.io/docs/graphql#strict-schemas-33ec7103289a .
Please feel free to share your schema folder or a link to your repo (in DM if you prefer) so I can have a look
🙂
user M
Yes, I have added a few fields and object. but can't seem to figure out which object needs to be top-level. I can DM you the repo link
Ah I see, you’ll have to “hoist” your
code
type field in
bodyPortableText
to the top level. To do so, the most simple way is probably to create a new object called `myCode.js`:
export default {
  name: 'myCode',
  title: 'Code Editor',
  description: 'Code editor',
  type: 'code'
}
And then include this as follows in `bodyPortableText`:

...
{
  type: 'myCode'
}
And add it to your
schema.js
file:
...
import myCode from './objects/myCode'
...
export default createSchema({
  ...
  types: schemaTypes.concat([
    ...
    myCode
    ...
  ])
})
user M
Thank you so much ❤️
I was looking for issues in an 'object' and completely missed out on the code component!!
Thanks a lot!
It's easy to miss, especially hidden like that inside the portable text array. Glad to hear it's working! 🚀

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?