🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with query string not returning results in Sanity client but working in Sanity Studio Vision

23 replies
Last updated: Jun 23, 2023
hello! the query string
 *[_type == "page" && upper(market) == upper($market)
    &&  _id == *[_type == "generalSettings" && upper(market) == upper($market)][0].home->_id
works correctly inside of sanity studio vision, but when fetching using the sanity client in-app with the exact same string, there are zero results. seems to be stemming from the nested

 _id == *[_type == "generalSettings" && upper(market) == upper($market)][0].home->_id
Jun 22, 2023, 5:41 PM
user U
. Two things I would check are (1) is Vision returning drafts, which you wouldn’t see in the client by default (unless it’s an authenticated request), and (2) are you using the same API versions in both (essentially, not
Jun 22, 2023, 5:49 PM
user A
Thanks so much, for real! Vision is not returning drafts, and the api version in both cases is v2
Jun 22, 2023, 5:52 PM
Sorry, in this case the API I’m referring to is for GROQ. Anything after
will be date-based—so something like
Jun 22, 2023, 5:53 PM
as far as I can tell, if i set all api versions in-app to 2023-06-22, and set my env var for api_version to 2023-06-22.
then run sanity studio, I have both 2021-10-21, and 2021-03-25 versions available. If i select and hit other, and put in 2023-06-22, I will get the same results.

But my api version now should be 2023-06-22 everywhere, and I still get the same results, working in sanity studio vision, not working with the client.
Jun 22, 2023, 6:03 PM
Thanks for confirming.
were both significant releases, which is why they’re included as defaults, but I’d expect both would give the same results as
given the query above.
Given that you’re still seeing different results between the client and Vision, can we perhaps see a bit more context of how you’re querying in the client? I.e., the whole
including params.
Jun 22, 2023, 6:05 PM
Course! Thanks so much. Very much appreciated.

// Takes `en-US` and returns `US`
export function getMarketFromNextLocale(locale: string) {
  return locale.split(`-`).pop().toUpperCase()

// Takes `en-US` and returns `en`
export function getLanguageFromNextLocale(locale: string) {
  return locale.split(`-`).shift()

export default async function PageSlugRoute({ params }) {
  const isDraftMode = draftMode().isEnabled
  let token = null
  // : PostsProps['data']
  if (isDraftMode) {
    if (!token) {
      throw new TypeError(`Missing SANITY_STUDIO_API_READ_TOKEN`)

  // // These query params are used to power this preview
  // // And fed into <Alert /> to create :sparkles: DYNAMIC :sparkles: params!

  // const pageData = await getPage({ slug: params.slug.join('/') })
  const pageData = await getPage({
    //   // Necessary to query for the right page
    // And used by the preview route to redirect back to it
    slug: params.slug.join('/'),
    // slug: params.slug,
    // This demo uses a "market" field to separate documents
    // So that content does not leak between markets, we always include it in the query
    market: getMarketFromNextLocale(params.locale) ?? `US`,
    // Only markets with more than one language are likely to have a language field value
    language: getLanguageFromNextLocale(params.locale) ?? null,
    // In preview mode we can set the audience
    // In production this should be set in a session cookie
    // audience:
    //   isDraftMode && previewData?.audience
    //     ? previewData?.audience
    //     : Math.round(Math.random()),
    // Some Page Builder blocks are set to display only on specific times
    // In preview mode, we can set this to preview the page as if it were a different time
    // By default, set `null` here and the query will use GROQ's cache-friendly `now()` function
    // Do not pass a dynamic value like `new Date()` as it will uniquely cache every request!

    // date: isDraftMode && previewData?.date ? previewData.date : null,
    date: null,

  console.log({ pageData })

  if (!pageData) {

  if (isDraftMode)
    return (
            <Page data={pageData} />
        <PagePreview token={token} slug={params.slug} />

  return <Page data={pageData} />
Jun 22, 2023, 6:08 PM
export async function getStaticPage({
}: {
  token?: string | null
  pageData: string
  market?: string
  language?: string | null
}): Promise<undefined> {
  const query = `
    "page": ${pageData},

  return await sanityClient(token)?.fetch(query, { market, language })
Jun 22, 2023, 6:10 PM
very confused
Jun 22, 2023, 10:19 PM
another example: working in studio, but not in the client.
*[_id == *[_type == "generalSettings" && upper(market) == upper($market)][0].home->_id]

Jun 23, 2023, 4:24 PM
Maybe params aren’t being passed in correctly? Can you try hardcoding
in the above (in your client) to see if that enables the query to work? By this, I mean that if your
param has a value of “spain”, try this query in the client:

*[_id == *[_type == "generalSettings" && upper(market) == upper("spain")][0].home->_id]
Jun 23, 2023, 4:27 PM
Thanks Geoff, but using the hardcoded "CA", or "US" market shows up results in vision, but not using the client. The id's that show in sanity are not drafts
Jun 23, 2023, 5:06 PM
any way I could give you the projectId and you could try the client?
Jun 23, 2023, 5:08 PM
Thanks for confirming. Yeah, please do. I can’t promise anything today but would like to take a look.
Jun 23, 2023, 5:14 PM
Jun 23, 2023, 5:14 PM
You say this query works in Vision? I can’t get results for the inner query (
*[_type == "generalSettings" && upper(market) == upper($market)][0]
) whether I replace
with “ca” or “us” .
Jun 23, 2023, 5:20 PM
*[_id == *[_type == "generalSettings" && upper(market) == upper("us")][0].home->_id]
Jun 23, 2023, 5:32 PM
Can you post the JSON you get back?
Jun 23, 2023, 5:33 PM
slug:{…} 2 properties
modules:[…] 1 item
0:{…} 4 properties
content:[…] 1 item
Jun 23, 2023, 5:33 PM
and then using just the inner query
*[_type == "generalSettings" && upper(market) == upper("us")][0]

{…} 7 properties
home:{…} 4 properties
_strengthenOnPublish:{…} 1 property
Jun 23, 2023, 5:36 PM
Jun 23, 2023, 5:37 PM
its a draft
Jun 23, 2023, 5:37 PM
ty for your help lol
Jun 23, 2023, 5:38 PM
You’re welcome! Glad you got it worked out!
Jun 23, 2023, 5:40 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?