Skip to content

Conditionally exclude filters #669

@valters-tomsons

Description

@valters-tomsons

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions