I’m trying to use defineQuery from next-sanity to build a query with dynamic filters based on user input. However, when building my Next.js project, I get this error:
✗ Unsupported expression type: ConditionalExpression in ./src/sanity/lib/products/getAllProducts.ts:XX:XX
const filters = `${categories?.length ? `&& count((categories[]->slug.current)[@ in $categories]) > 0` : ""}
${occasion ? `&& occasion == $occasion` : ""}
${stone ? `&& $stone in stone[]` : ""}`;
const ALL_PRODUCTS_QUERY = defineQuery(
`*[_type == 'product' ${filters}] {
...,
"categories": categories[]->slug.current
}`
);
It seems like defineQuery does not support dynamically constructed queries. Is this a known limitation? If so, what’s the best way to handle dynamic filtering in Sanity queries? Should I avoid defineQuery and pass a plain string query to sanityFetch instead?
Any guidance would be appreciated!