Issue with getting data from an array in Sanity to Gatsby, but problem was resolved.

6 replies
Last updated: Apr 1, 2020
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.

acccessibilityAuditCollection is a document because I tried to change it to a document, it was originally an object and I’ve changed it back to an object, I saw no change from changing between object and document 🙂
Hey Andreas! Your document containing the
auditLevels
array is called
accessibilityAnalysis
, and it’s a document type, correct?
Have you tried adapting the query as follows?

allSanityAccessibilityAnalysis {
  edges {
    node {
      auditLevels {
        auditTitle
        auditButton
      }
    }
  }
}
Also, if the dataset is public and you have a playground, could you share the URL (in DM if you prefer)?
🙂
Btw, I see a spelling difference in your schema/queries between
acccessibilityAuditCollection
(acccessibility with 3x ‘c’) and
allSanityAccessibilityAnalysis
(accessibility with 2x ‘c’). Maybe that’s causing this?
Hello, There is only one of this document so I thought I did not need to use the “all” queries from graphiql, but as long as it works
Problem is that it still does not work, I tried your query, it did not work. “allSanityAccessibilityAnalysis” is the only accessibilityanalysis option avaialbe to my from graphiQL for the “all” options.

Been trying since yesterday to make this query work and have changed it allot during that time, but I just can’t get it to work at all
I solved the isseu, thank you Peter! Not sure how I solved it.I created new names for the auditLevels object, added new data, ran graphql deploy again and now it works
🙂
Looks like fortune is smiling upon you 😄 Glad you got it to work!

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?