Gatsby shows empty Sanity children but playground shows data

11 replies
Last updated: May 12, 2020
Is this an issue where Gatsby shows empty sanity children but the Sanity playground shows data?
AI Update

This is indeed a known issue with Gatsby and gatsby-source-sanity when working with Portable Text! The problem occurs because of a field name conflict between Gatsby's internal GraphQL schema and Sanity's Portable Text structure.

The Root Cause

Portable Text blocks in Sanity have a children field that contains the actual text content (spans, marks, etc.). However, Gatsby reserves the children field name for its own internal node relationships in GraphQL. When gatsby-source-sanity tries to map Sanity's Portable Text structure to Gatsby's GraphQL layer, this conflict causes the children field to appear empty in Gatsby's GraphQL (GraphiQL), even though the data exists perfectly fine in Sanity's Vision/Playground.

The Solution: Use _rawFields

The gatsby-source-sanity plugin generates special _raw prefixed fields specifically to handle this and other similar issues. These fields provide the raw JSON data from Sanity without Gatsby's GraphQL transformations interfering.

Instead of querying your Portable Text like this:

query {
  sanityPost {
    body {
      children {
        text
      }
    }
  }
}

Query it using the _raw version:

query {
  sanityPost {
    _rawBody
  }
}

The _rawBody field (or _rawYourFieldName for whatever your Portable Text field is called) will contain the complete, unmodified Portable Text structure with all the children arrays intact.

Rendering the Raw Portable Text

Once you have the raw data, you'll need to use a Portable Text renderer like @portabletext/react to display it:

import {PortableText} from '@portabletext/react'

function MyComponent({data}) {
  return <PortableText value={data.sanityPost._rawBody} />
}

You can also configure reference resolution depth in your GraphQL query if your Portable Text contains references:

query {
  sanityPost {
    _rawBody(resolveReferences: {maxDepth: 5})
  }
}

This is a documented feature of gatsby-source-sanity - the _raw fields are specifically designed to bypass Gatsby's GraphQL transformations and give you direct access to Sanity's data structure, which is essential for Portable Text and other complex nested content.

Show original thread
11 replies
If you want to access the sanity children you would need to query the
_raw
version of that field. So in your case,
_rawAmenities
and you’ll get the children.
Thank! you! Just got to that a couple minutes ago, it's working
Do you know any trick to fetch the data without the need to stop the Gatsby server?
On your gatsby-config. Add
watchMode: true
on your gatsby-sanity-source options. That should turn live preview!
Wow, thank you very much!
🙌
You can also show drafts by adding
overlayDrafts: true
more info from the source plugin: https://github.com/sanity-io/gatsby-source-sanity
It’s a really cool feature! It disciplined me to write code that doesn’t break the frontend if there is any missing data!
Nice, actually that's an excellent tip
write code that doesn't break the frontend if there is any missing data
Thank you
user N
You’re welcome! Happy to help1

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?