TracksMastering Content OperationsCoursesBetween GROQ and a hard placeQuery parameters
Between GROQ and a hard place
Markdown Version

Query parameters

Log in to watch a video walkthrough of this lesson
Log in
Video thumbnail
Use parameters as variables in your queries, whether they have values or not.
Log in to mark your progress for each Lesson and Task

The most common use case for using parameters in GROQ for front ends is matching a document against its slug, using a dynamic value from your front end framework.

Query for a single event with this matching slug. If you used the seed data in a previous course this document should exist.
*[
_type == "event"
&& slug.current == $slug
][0]
{
"slug": "the-national-at-the-bowery-ballroom"
}

This should return a single document.

Using parameters in GROQ queries is superior and safer than string concatenation in JavaScript. In the bad example below the variable needs to be wrapped in quotes and can be easily formatted incorrectly.

// ❌ Avoid doing this, use params instead!
const EVENT_QUERY = `*[
_type == "event"
&& slug.current == "${slug}"
][0]`

You can use select to conditionally use a parameter to filter documents.

Query to return the name of future events if a date is provided, or all events if not.
*[
_type == "event"
&& select(
defined($date) => date > now(),
true
)
].name
{
"date": "2025-07-04T06:44:33.014Z"
}

Select will return the first item that returns true. So the first condition uses defined() to check if $date is not null

Query again without a date value
{
"date": null
}

Otherwise, it returns true which will have no impact on the filter because all documents satisfy the condition.

Note that a parameter cannot be undefined, so if you declare a parameter in a query which could be missing, it must be present in the supplied parameters as null.

You have 3 uncompleted tasks in this lesson
0 of 3