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

Difficulty with GROQ query for getting a reference for an image 2 levels down.

8 replies
Last updated: Feb 7, 2022
Hello, I’m running into some difficulties with a
GROQ
query getting a reference for an image 2 levels down (a reference in a reference).
My query looks like this:

"footerNavSocial": footerNavSocial->social[]{
  "alt": icon.alt,
  "image": icon.asset,
  link,
  name,
}
And it provides me with this data:

[
  {
    "alt": "Facebook",
    "image": {
      "_ref": "image-cecf0ea498a09fd23ed9d905abe6e48ba86b9eae-10x20-svg",
      "_type": "reference"
    },
    "link": "<https://www.facebook.com/timetap.onlinescheduling>",
    "name": "Facebook"
  },
  {
    "alt": "Twitter",
    "image": {
      "_ref": "image-26c444e906d4dbee7f5e14fec5f0adfcaf6d3849-20x16-svg",
      "_type": "reference"
    },
    "link": "<https://twitter.com/tmtap>",
    "name": "Twitter"
  },
  {
    "alt": "LinkedIn",
    "image": {
      "_ref": "image-daf93fd05aaf6980627b678ce4428b9ebf2d8130-19x19-svg",
      "_type": "reference"
    },
    "link": "<https://www.linkedin.com/company/timetap>",
    "name": "LinkedIn"
  }
]
It’s late, so maybe I’m missing something obvious, but I’m hoping I can get some pointers on how to grab the image URL. I tried this (and many other variations):

"footerNavSocial": footerNavSocial->social[]{
  "alt": icon.alt,
  "image": icon.asset->,
  link,
  name,
}
And that gave me an empty object and so I got no data for the “image” value

[
  {
    "alt": "Facebook",
    "link": "<https://www.facebook.com/timetap.onlinescheduling>",
    "name": "Facebook"
  },
  {
    "alt": "Twitter",
    "link": "<https://twitter.com/tmtap>",
    "name": "Twitter"
  },
  {
    "alt": "LinkedIn",
    "link": "<https://www.linkedin.com/company/timetap>",
    "name": "LinkedIn"
  }
]
Feb 4, 2022, 11:32 PM
Hi
user M
thanks for getting back to me. The full query is:
*[_type == "footerNav"][0]{"footerNavSocial": footerNavSocial->social[]{
  "alt": icon.alt,
  "image": icon.asset->,
  link,
  name,
}}
The query URL is:
https://gyc8zs33.api.sanity.io/v1/data/query/production?query=*%5B_type%20%3D%3D%20%22fo[…]20%20%20%20name%2C%0A%20%20%20%20%20%20%20%20%20%20%7D%7D
Feb 7, 2022, 2:35 PM
Hi
user M
the full query is:
Feb 7, 2022, 2:35 PM
I’m not sure if it’s relevant, but I also tried changing the API version in Vision to match the API version I have set in my options for
@sanity.client
which is
apiVersion: '2021-01-18'
.
The Vision query URL for that is:
<https://gyc8zs33.api.sanity.io/v2021-01-18/data/query/production?query=*%5B_type%20%3D%3[…]20%20%20%20name%2C%0A%20%20%20%20%20%20%20%20%20%20%7D%7D>
Feb 7, 2022, 2:40 PM
Oh, I also want to mention these files are SVGs
Feb 7, 2022, 3:21 PM
Oh wow, looks like I had a silly typo on my apiVersion string, it should’ve said `2022-01-18`LOL.
I discovered that by accidentally testing my GROQ query in Vision with
v2021-03-25
Feb 7, 2022, 3:48 PM
Did changing the version also solve the issue with the reference as well?
Feb 7, 2022, 5:40 PM
Yes it did!
Feb 7, 2022, 6:41 PM
Perfect!
Feb 7, 2022, 6:51 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?