👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Troubleshooting a groq query with a reference in Sanity.io

26 replies
Last updated: Nov 12, 2021
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;
  *[_type == "page" && slug.current == 'values-and-beliefs'] {
    blocks[] {"slug": link->slug.current, ...},
    ...
  }

link
here is a
reference
and I'm trying to expand it out. In Vision, I can see a new field called
slug
with 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
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.
v1
vs.
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
• 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
Nov 8, 2021, 7:53 PM
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
I confirmed the version is
2021-10-29
but 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
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-29
to match them identically.
If I’ve misunderstood please let me know.
Nov 9, 2021, 3:58 PM
If that’s what’s returned from
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-29
to match them identically.
If I’ve misunderstood please let me know.
Nov 9, 2021, 3:58 PM
No that sounds right Geoff but I'm not sure it ultimately helps me solve the original problem
Nov 9, 2021, 4:00 PM
Thanks for the reply of course
Nov 9, 2021, 4:00 PM
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?
Nov 9, 2021, 5:51 PM
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
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
user A
apologies for the delayed response
Nov 12, 2021, 10:27 AM
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
Is there any way of looking up/resolving a
_ref
using JavaScript instead of groq? Something like
resolveRef(_ref)
which would return a slug or something?
Nov 12, 2021, 11:02 AM
Is there any way of looking up/resolving a
_ref
using JavaScript instead of groq? Something like
resolveRef(_ref)
which would return a slug or something?
Nov 12, 2021, 11:02 AM
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
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
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
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
Got it 😂 what a rollercoaster...

    *[_type == "page" && slug.current == $slug] {
      blocks[_type == 'sectionMarker'] {
        'slug': link->slug.current,
        ...
      },
      ...,
    }
Nov 12, 2021, 2:52 PM
Hah! Glad you got it. You can also do this to handle multiple types:
blocks[] {
  ...,
  _type == "sectionMarker" => {
    "slug": link->slug.current,
  },
  _type == "somethingElse" => { ... }
}
edit: added missing
[]
Nov 12, 2021, 2:55 PM
Yeah that's neater, thanks Derek
Nov 12, 2021, 2:56 PM
I had to change it to
blocks[] {
but it worked
Nov 12, 2021, 3:01 PM
ah oops my bad — thanks for correcting that!
Nov 12, 2021, 3:02 PM
No problem! Easy for me when I can test it
Nov 12, 2021, 3:07 PM
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.

Was this answer helpful?