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
K
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:
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'},
],
}],
},
],
}
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
J
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
K
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
A
In your GraphQL Playground, does it show you any other fields?
Jan 31, 2021, 5:45 PM
K
Nope, only those...
Jan 31, 2021, 5:45 PM
A
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
A
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
A
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
J
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
A
query { allSanityGrid { nodes { items { __typename … on track { # fields } __typename … on link { # fields } __typename … on picture { # fields } __typename … on journal { # fields } } } } }
https://graphql.org/learn/queries/#meta-fields
Jan 31, 2021, 6:18 PM
Q
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
K
No problem
user L
, thanks a lot!Jan 31, 2021, 9:12 PM
K
Thanks a lot
user A
, I'm going to try that.Jan 31, 2021, 9:14 PM
K
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
Q
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,
linkand
pictureare 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
K
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
K
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
G
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.