How I can return everything(list) if filters is empty string? [GraphQL Query using filters]

System Information
  • Strapi Version: 4.9.2
  • Operating System: MacOs 13.3.1
  • Database: sqlite
  • Node Version: 18.13.0
  • NPM Version: 9.4.0
  • Yarn Version: 1.22.19

Hello community, I am glad to have joined the forum.
I am using strapi as backend for my GF blog, and while I was trying to implement a search functionality I incurred in a few issues.

Before deciding to write here, I did a deep search both on this forum than in google, and I couldn’t find anything except another post on this forum but without any answers unfortunately (Return everything if filter variable is empty?).

So I post my question here.

I do have a very simple query (every blog post has assigned a category - you can pick the category from a dropdown in strapi server when assign it. It’s an attribute not a different model):

query getPosts($category: String!) {
  posts(filters: { category: { eq: $category } }) {
    data {
      id
      attributes {
        category
      }
    }
  }
}

When I use as variable any category that I have, the query returns the correct result. But if instead I leave the variable empty (“category”: “”), then Strapi return an empty array.
What I would like is that in the case of an empty variable, Strapi returns all the posts, in other words it ignores completely the filters.
So that I am able to use the same query to fetch all the available blogs (the get list action), or filters them using the category as query parameter.
Is this possible? or I need to write distinct queries?

By default the blog loads all the posts (paginated clearly), and you can filter them down pressing a button.

Looking forward to getting some help (I am a bit stuck at the moment).
Thank you guys:)

EDIT

I can obtain what I want if instead of eq I use contains as condition in the filters.

query getPosts($category: String!) {
  posts (filters: { category: { contains: $category }} ){
        data {
          id
          attributes {
            category
          }
        }
      }
    }

This works but I would like to have the eq condition instead.