Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Is it possible to have a conditional value within groq?

3 replies
Last updated: Oct 19, 2020
Is it possible to have a conditional value within
groq
?
EX: If a user sets an unique DATE we should use that date instead of
_updatedAt
value?
Would love to do this on the groq side so the
date
property always has a value.
Oct 19, 2020, 3:59 PM
Hi pbj, you could use the
coalesce()
function for this. It takes a number of attribute references and returns the value of the first attribute that is non-null. So if you’d like to default to the
_updatedAt
value if no
date
exists, this should work:
*[_type == 'post']{
  ...,
  'date': coalesce(date, _updatedAt)
}
Oct 19, 2020, 4:21 PM
Hi pbj, you could use the
coalesce()
function for this. It takes a number of attribute references and returns the value of the first attribute that is non-null. So if you’d like to default to the
_updatedAt
value if no
date
exists, this should work:
*[type == 'post']{
  ...,
  'date': coalesce(date, _updatedAt)
}
Oct 19, 2020, 4:21 PM
user M
thank you!
Oct 19, 2020, 4:26 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?