✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Handle Certain Values To Be True

By James Singleton & Geoff Ball

If you want to pass a parameter to a query that might not always be defined

Query

*[_type == 'post' && select(
  $id == null => true,
  _id == $id
)]

I ran into an issue where sometimes I would be passing a parameter into a groq query that wasn't defined. For example, if you had a query param ?category=foo to help narrow down a search result. If you wanted to go back to seeing all of that search result, it would be nice (in my opinion) to not have to call a completely separate query.

Contributors

Other schemas by authors

Matching the end of a string in GROQ

GROQ doesn't yet include a function to match the end of a string, but we can simulate it by splitting our string on our term.

Geoff Ball
Go to Matching the end of a string in GROQ