Querying nested array of documents in Sanity/Gatsby GraphQL

6 replies
Last updated: Jan 28, 2026
Hello, I am trying to get some data from an array (and then array data from that array) in sanity to Gatsby, but I can’t figure out how to get the data in GraphiQL
I’m calling my object containing an array in my document like so

{

title: "Audit levels",

name: "auditLevels",

type: "array",

of: [{ type: "acccessibilityAuditCollection" }]

},

my accessibilityAuditCollection looks like this

export default {

name: "acccessibilityAuditCollection",

title: "Accessibility audit product",

type: "document",


fields: [

{

name: "auditTitle",

title: "Title",

type: "string"

},

{

title: "Most sold product",

name: "released",

type: "boolean"

},

{

title: "Audit feature",

name: "smallAuditFeatures",

type: "array",

of: [{ type: "accessibilityAuditProductFeatures" }]

},

{

name: "auditButton",

title: "Audit level button text",

type: "string"

}

]

};

I can get all the data from my document except for the array object data, I’ve got this query in GraphiQL now (trying both all and the normal document fetch method)


query coursesPageQuery {

sanityAccessibilityAnalysis {

_rawAuditLevels

auditLevels {

auditButton

auditTitle

}

}

allSanityAccessibilityAnalysis {

nodes {

auditLevels {

auditTitle

auditButton

}

}

}

}
None of the fields return anything, any ideas? I’ve filled inn data into sanity (and ran graphql deploy)
AI Update

Looking at your schema setup, I can see the issue - you're using type: "document" for acccessibilityAuditCollection, but you're referencing it within an array field. This creates a mismatch in how Gatsby's GraphQL layer interprets the data.

When you have an array of documents in Sanity, you're essentially creating references to other documents, not embedding them directly. The gatsby-source-sanity plugin needs to understand whether you're embedding objects or referencing documents.

Here's what you need to fix:

Change your schema structure

You have two options:

Option 1: Use an object type instead of document (recommended for your use case)

Change acccessibilityAuditCollection from type: "document" to type: "object":

export default {
  name: "acccessibilityAuditCollection",
  title: "Accessibility audit product",
  type: "object", // Changed from "document"
  fields: [
    {
      name: "auditTitle",
      title: "Title",
      type: "string"
    },
    {
      title: "Most sold product",
      name: "released",
      type: "boolean"
    },
    {
      title: "Audit feature",
      name: "smallAuditFeatures",
      type: "array",
      of: [{ type: "accessibilityAuditProductFeatures" }]
    },
    {
      name: "auditButton",
      title: "Audit level button text",
      type: "string"
    }
  ]
};

Option 2: Use references if they need to be separate documents

If you want them to remain as separate documents (for reusability across multiple pages), change your array definition to use references:

{
  title: "Audit levels",
  name: "auditLevels",
  type: "array",
  of: [{ 
    type: "reference",
    to: [{ type: "acccessibilityAuditCollection" }]
  }]
}

After changing the schema

  1. Redeploy your GraphQL schema: sanity graphql deploy
  2. Clear Gatsby's cache: gatsby clean
  3. Restart your development server

Your GraphQL query should then work. If you used references (Option 2), your query would need to look slightly different to traverse the reference relationship.

The key insight here is that in Sanity, documents are top-level content types that live independently in your content lake, while objects are embedded data structures. For nested array data like you're describing, objects are typically the right choice unless you need to reuse the same audit collection across multiple pages.

Show original thread
6 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.

Was this answer helpful?