I am currently working with a GraphQL query using Apollo Client JS that looks like this:
query GetUsers($searchFilter: String) {
users(
first: 10,
filter: { search: $searchFilter }
) {
nodes {
id
name
}
}
}
Everything functions as expected when I provide the $searchFilter
argument. However, I would like to make this argument optional. In other words, if it is null
, the filter should not be applied.
While this may seem straightforward, the API mandates that the search
field be non-nullable. Therefore, passing in filter: { search: null }
is not permissible.
My goal is to achieve the following modification:
query GetUsers($searchFilter: String) {
users(
first: 10,
filter: $searchFilter = null ? null : { search: $searchFilter }
) {
nodes {
id
name
}
}
}
How can I conditionally add the filter
argument?