Troubleshooting a groq query with a reference in Sanity.io
26 replies
Last updated: Nov 12, 2021
C
Good morning everyone. I have a groq query with a reference that works in the 'vision' tab, but not in my code for some reason;
Using Next
*[_type == "page" && slug.current == 'values-and-beliefs'] { blocks[] {"slug": link->slug.current, ...}, ... }
linkhere is a
referenceand I'm trying to expand it out. In Vision, I can see a new field called
slugwith the value of whatever the reference is (
about,
news, etc), but in my code I don't see the same field
Using Next
Nov 8, 2021, 8:55 AM
D
hey nice to see you’re using Next now! Re: difference between vision & code, check for these:• Are you querying against the same API version? (e.g.
• On vision, drafts can show up in query results while on the frontend you’d need to set a API token for the Sanity client. Are you looking at the same document on both vision & your frontend?
v1vs.
2021-03-25)• Same dataset?
• On vision, drafts can show up in query results while on the frontend you’d need to set a API token for the Sanity client. Are you looking at the same document on both vision & your frontend?
Nov 8, 2021, 9:06 AM
C
• I'm pretty sure the api is the same but how can I check? My sanity config is using a date I set a few weeks ago• Same dataset definitely
• All the documents are published so they shouldn't be drafts
• All the documents are published so they shouldn't be drafts
Nov 8, 2021, 7:53 PM
C
Thanks for the reply
user G
sorry it took me awhile to respond, had some bigger issues I had to jump on instead 😓Nov 8, 2021, 7:54 PM
C
I confirmed the version is
2021-10-29but how can I confirm this is the same API version I'm querying against?
Nov 9, 2021, 8:23 AM
If that’s what’s returned from
If you mean the version you’re querying against in Vision, that’s set via the dropdown in the top-left. You can choose Other and then enter
If I’ve misunderstood please let me know.
client, that’s the version of the API it’s using when it queries.
If you mean the version you’re querying against in Vision, that’s set via the dropdown in the top-left. You can choose Other and then enter
2021-10-29to match them identically.
If I’ve misunderstood please let me know.
Nov 9, 2021, 3:58 PM
If that’s what’s returned from
If you mean the version you’re querying against in Vision, that’s set via the dropdown in the top-left. You can choose Other and then enter
If I’ve misunderstood please let me know.
client, that’s the version of the API it’s using when it queries.
If you mean the version you’re querying against in Vision, that’s set via the dropdown in the top-left. You can choose Other and then enter
2021-10-29to match them identically.
If I’ve misunderstood please let me know.
Nov 9, 2021, 3:58 PM
C
No that sounds right Geoff but I'm not sure it ultimately helps me solve the original problem
Nov 9, 2021, 4:00 PM
C
Thanks for the reply of course
Nov 9, 2021, 4:00 PM
C
My reference expands in Vision, but not in my code when I
console.log()the returned data
Nov 9, 2021, 4:01 PM
You’re right—it doesn’t. That said, now that we know it’s not a version issue, we can take a look back at the original issue.
What are you seeing returned from that query on your front end?
What are you seeing returned from that query on your front end?
Nov 9, 2021, 5:51 PM
C
So regarding the data mismatch between the code and Vision, I get this when I run the query in Vision:
Nov 12, 2021, 10:27 AM
C
But I get this in the code (note the key is the same, but it's missing the 'slug' parameter)
Nov 12, 2021, 10:27 AM
C
user A
apologies for the delayed responseNov 12, 2021, 10:27 AM
C
I tried a whole other query just to see if I could get the results another way. Again this works in Vision but not in the code itself;
*[_type == "page" && slug.current == $slug] { blocks[_type match 'sectionMarker']{'slug': link->slug.current, ...}, ... }
Nov 12, 2021, 11:00 AM
C
Is there any way of looking up/resolving a
_refusing JavaScript instead of groq? Something like
resolveRef(_ref)which would return a slug or something?
Nov 12, 2021, 11:02 AM
C
Is there any way of looking up/resolving a
_refusing JavaScript instead of groq? Something like
resolveRef(_ref)which would return a slug or something?
Nov 12, 2021, 11:02 AM
C
Further details on this... I realised that my API version in 'Vision' was set to 'v1' — I just set it to 'v2021-03-25' and now the query above isn't working. So
The question now is what is the correct query?
user A
and user G
were correct, it WAS an API version issue (sorry both!) ...The question now is what is the correct query?
Nov 12, 2021, 2:41 PM
C
Further details on this... I realised that my API version in 'Vision' was set to 'v1' — I just set it to 'v2021-03-25' and now the query above isn't working. So
The question now is what is the correct query?
user A
and user G
were correct, it WAS an API version issue (sorry both!) ...The question now is what is the correct query?
Nov 12, 2021, 2:41 PM
C
Got it 😂 what a rollercoaster...
*[_type == "page" && slug.current == $slug] { blocks[_type == 'sectionMarker'] { 'slug': link->slug.current, ... }, ..., }
Nov 12, 2021, 2:52 PM
D
Hah! Glad you got it. You can also do this to handle multiple types:
edit: added missing
blocks[] { ..., _type == "sectionMarker" => { "slug": link->slug.current, }, _type == "somethingElse" => { ... } }
[]
Nov 12, 2021, 2:55 PM
C
Yeah that's neater, thanks Derek
Nov 12, 2021, 2:56 PM
C
I had to change it to
blocks[] {but it worked
Nov 12, 2021, 3:01 PM
D
ah oops my bad — thanks for correcting that!
Nov 12, 2021, 3:02 PM
C
No problem! Easy for me when I can test it
Nov 12, 2021, 3:07 PM
C
No problem! Easy for me when I can test it
Nov 12, 2021, 3:07 PM
Hey, no problem! Glad you got it resolved in the end, and thanks (again) Derek for the support.
Nov 12, 2021, 4:27 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.