how to give optional filtering parameters in sanity groq

Viewed 25

I have a schema of Cases in my sanity docoments Suppose it have fields:

title,
category,
country,
state,
district
date,

Now i have the use case where the user can query data using all the fields depends on his needs Here is the query which i am making and it works fine but the issue comes to when the user is not giving country state or city or the fields where i need text they becomes equal to empty strings to its expected that it will give me blank result but

Is there any way where i can pass the optional arguments like if they exist they will be in the query else they will not. I have the option to use if else like that exist make this query but i have this feeling i am doing it wrong. what is correct way to achieve it?

*[_type == "cases" && title match "man" && date < "2022-10-07" && country == "INDIA" && state == "JHARKHAND" && district == "DUMKA"]
{title,slug,description,country,state,district,date,mainImage{asset->{url}}} 
0 Answers
Related