0

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!

ryansle
  • 251
  • 2
  • 14
  • 1
    Can you explain a bit more? Are you trying to extend the `fetchExperience()` query, or write a new one? And how do experiences reference skills? You can do `'techStack': stack[]-> { technology, icon }` to get the specific fields, but I'm not sure if this is what you want. – Alexander Staubo Feb 04 '22 at 22:34
  • @AlexanderStaubo that was exactly what I was looking for, lol. If you want to answer the question, I'll mark your answer as the accepted solution! – ryansle Feb 08 '22 at 23:50

1 Answers1

1

You can do this to get the specific fields:

"techStack": stack[]-> { technology, icon }
Alexander Staubo
  • 3,148
  • 2
  • 25
  • 22