Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Hi everyone, hope you can help me out. I have an issue working with Gatsby and Sanity. When querying for an array of references I do get the list of references with the fields...

17 replies
Last updated: Feb 1, 2021
Hi everyone, hope you can help me out. I have an issue working with Gatsby and Sanity. When querying for an array of references I do get the list of references with the fields _key, _ref, _type but instead I would like to retrieve a list of the actual documents. Is it possible to do that? Thanks 🙂 Below the schema:

export default {
    name: 'grid',
    title: 'Grid',
    type: 'document',
    fields: [
        {
            name: 'name',
            title: 'Name',
            type: 'string',
        },
        {
            name: 'items',
            title: 'Items',
            type: 'array',
            of: [{
                type: 'reference',
                to: [
                    {type: 'track'},
                    {type: 'link'},
                    {type: 'picture'},
                    {type: 'journal'},
                ],
            }],
        },
    ],
}
Jan 31, 2021, 5:23 PM
Hey
user K
, are you using groq? Can you share your query that is only returning _key, _ref and _type?
Jan 31, 2021, 5:31 PM
Hey
user J
thanks for the reply 🙂 I'm using Gatsby with the gatsby-source-sanity plugin. Under items I only get the reference fields (_key, _ref, _type). The graphql query:
query {

allSanityGrid {

nodes {

items {

_key

_ref

_type

}

}

}

}
Jan 31, 2021, 5:40 PM
In your GraphQL Playground, does it show you any other fields?
Jan 31, 2021, 5:45 PM
Nope, only those...
Jan 31, 2021, 5:45 PM
I don’t know much GraphQL, so please bear with me, but it seems like it might have something to do with that array of multiple references.
Jan 31, 2021, 5:59 PM
I just spun up a project using GraphQL and can access a lot more when it’s a reference to just one type than when I added a second one.
Jan 31, 2021, 5:59 PM
Now, I wish I could answer where you might go from there (if that’s even on the right track), but you might be able to confirm by getting rid of three of the four reference types and seeing if it then works for you.
Jan 31, 2021, 6:00 PM
user K
sorry - I'm not well versed with GraphQL and I'm not sure I can help you there!
Jan 31, 2021, 6:07 PM
query {
  allSanityGrid {
    nodes {
      items {
        __typename
        … on track {
          # fields
        }
        __typename
        … on link {
          # fields
        }
        __typename
        … on picture {
          # fields
        }
        __typename
        … on journal {
          # fields
        }
      }
    }
  }
}
This seemed to work for me, though I don’t know if it’s the right approach. I read about it here:
https://graphql.org/learn/queries/#meta-fields
Jan 31, 2021, 6:18 PM
user K
are you querying outside of normal page components? e.g. are you trying to query in gatsby-node or something like that? I have run into this problem, but only when querying in odd spots. It has to do with how the plugin resolve references for you, and where this happens in the build pipeline for Gatsby. When I have run into this problem, for instance when doing custom node creation in gatsby-node then I have dealt with it by just using sanityClient and a Groq query as it more gracefully handles the references for you.
Jan 31, 2021, 6:28 PM
No problem
user L
, thanks a lot!
Jan 31, 2021, 9:12 PM
Thanks a lot
user A
, I'm going to try that.
Jan 31, 2021, 9:14 PM
Hi
user Q
. No, I'm querying on the index page so nothing weird there. I will keep trying tomorrow and will test the same on a different project just to double check. Hopefully tomorrow I'll let you know what was the issue. Thanks for your help.
Jan 31, 2021, 9:18 PM
user K
I assume you have properly scoped/hoisted GraphQL for your document. If you read about GraphQL and how the schema is generated from your SANITY schema you need to make sure the
track
,
link
and
picture
are all their own schema types. That is the only other thing I can think of. https://www.sanity.io/docs/graphql#strict-schemas-33ec7103289a
Feb 1, 2021, 4:25 AM
Hey
user Q
, yes I did all that. I also tried the meta fields suggestion
user A
did and it perfectly worked on the Sanity Playground, but then when I try to replicate the query on Gatsby it doesn't . Thanks for your help 🙂
Feb 1, 2021, 7:23 AM
Hopefully this can be helpful to anybody going through the same situation: As I mentioned on my previous comment, the meta field queries were working on the Sanity Playground but not on Gatsby as the Sanity*** docs were not recognized. I ended up removing and reinstalling the gatsby-source-sanity plugin and that fixed the issue 🤷‍♂️ Thanks
user Q
and
user A
for your help 🙂
Feb 1, 2021, 9:40 AM
Glad you got it working, Viso!
Feb 1, 2021, 3:19 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing