πŸ‘€ See Sanity in action: Watch product demo now β†’

What is the recommended way to deal with something like role in GROQ which can return different things?

5 replies
Last updated: Feb 5, 2021
GROQ question, what is the recommended way to deal with something like
role
which can return different things?
Feb 5, 2021, 10:31 AM
Is it role[] -> _type role || someOtherDocType?
role[] -> {
  _type == 'role' => @{
    label
  }
  _type == 'other' => @{
    name
  }
}
The above could handle the different documents you reference.
Feb 5, 2021, 11:22 AM
Interesting let me try
Feb 5, 2021, 12:00 PM
I ended up with this, maybe there is something better πŸ€”
_type == "person" => {
   _id,
   name,
   role[]
  },
 _type == "institution" => {
   _id,
   name,
   role[]->{name}
}

Feb 5, 2021, 12:41 PM
I would maybe only use references and not strings for a more predictable editor, but if it works :-)
Feb 5, 2021, 2:12 PM
Good point! thanks
user G
Feb 5, 2021, 2:17 PM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in