Search code examples
reactjssanity

Sanity.io Fetching Through References


I'm trying to figure out how to access a full object reference through Sanity.io. Example code:

const fetchExperience = async () => {
  const data = await sanityClient.fetch(`*[_type == 'experience'] {
    company,
    title,
    'techStack': stack[]->technology,
    'imageUrl': image.asset->url,
  }`);

  return data;
};

I have another schema declared called Skill that contains two fields: technology, and icon. This Experience schema that I am working with above and references the Skill schema. Using this post, I was able to figure out how to grab a singular field through the reference and populate a field called techStack. However, I want to take this one step further: how can I write a GROQ that returns me an array of objects that might look like this?:

[
  { technology: 'React.js', icon: 'pretend-this-is-a-link-to-an-asset' },
  ...
]

Thanks in advance!


Solution

  • You can do this to get the specific fields:

    "techStack": stack[]-> { technology, icon }