graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Conditionally exclude filters

Open valters-tomsons opened this issue 6 months ago • 0 comments
trafficstars

Issue

Some GraphQL APIs don’t handle optional filters properly — when a filter is not needed, it should be omitted from the query. Instead, we’re forced to rebuild the query string if we don’t need that filter.

I want to reuse a query like this:

query($page: Int!, $size: Int!, $categoryId: String) {
  items(page: $page, size: $size, filter: { category: { eq: $categoryId } }) {
    id
  }
}

If categoryId is not provided, it should be removed from the query entirely, not passed as null.

Request

Is there a better way to handle this that I’m missing?

It would be helpful if GraphQLRequest could:

  • Allow us to conditionally include/exclude filters based on the variable values.
  • Provide a way to compose queries without having to rebuild them from scratch each time.

valters-tomsons avatar May 09 '25 13:05 valters-tomsons