Join us live Sept. 27 – How Sanity and Vercel powered Morning Brew's transformation –>

Is there a way to get the key in an array when resolving the references?

6 replies
Last updated: Sep 10, 2020

Is there a way to get the key in an array when resolving the references?

*[_type == "category"] {
events[]-> {_key, _id}
}
I cant find a way to include the
_key
here.

Sep 10, 2020, 9:41 AM

Could you post an example of a category document? Easier to help when seeing the document structure

Sep 10, 2020, 9:59 AM

{
  "events": [
    {
      "_key": "f9d29ec438bf",
      "_ref": "5b94ed5c-064d-4fe8-bafe-e53920984f65",
      "_type": "reference"
    }
  ]
}

Sep 10, 2020, 10:11 AM

events[]{
 _key, 
  @->
}
Perhaps?

Sep 10, 2020, 10:27 AM

Getting close 🙂 I needed to put it in an object.

*[_type == "category"][0] {
events[] {
  _key,
  "something": @-> {...}
}
}
Any ideas how to make attach it to the parent?

Sep 10, 2020, 10:31 AM

  events[]{
    _key,
    ...@->
  }

Sep 10, 2020, 10:32 AM

Nice! Thanks ❤️

Sep 10, 2020, 1:15 PM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.