Unsupported expression type: ConditionalExpression” for Dynamic Filters

Unsupported expression type: ConditionalExpression” for Dynamic Filters

Publish Date: Mar 9
0 0

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

Enter fullscreen mode Exit fullscreen mode

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
  }`
);

Enter fullscreen mode Exit fullscreen mode

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!

Comments 0 total

    Add comment