Unlock seamless workflows and faster delivery with our latest releases- get the details

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

Sanity is a 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.

Was this answer helpful?