how to filter in the dynamic zone in strapi using graphql?
hello, dev's. i need help in strapi. if any one know please help me! thanks 🙂
query GetPosts($datelte: Date!, $dategte: Date!, $searchkey: String) {
posts(filters: { effectiveDate: { lte: $datelte, gte: $dategte } }) {
data {
attributes {
effectiveDate
contentSections {
... on ComponentElementsEvents {
title
}
}
}
}
}
}
here how can I add a search filter by searchkey for the title?
title it's inside a dynamic zone.
Hi there,
The code snippet that you've shared does not seem to be correct, it includes a bunch of span tags. Can you re-share the code snippet that only contains your JS code?
1
@bobbyiliev Can you help me if you read the article again?
Hey!
Not 100% sure, but based on the information that you've provided, In Strapi, you'll need to use a nested filter with the elemMatch operator:
You should be able to modify the GraphQL query like this:
query GetPosts($datelte: Date!, $dategte: Date!, $searchkey: String) {
posts(filters: { effectiveDate: { lte: $datelte, gte: $dategte }, contentSections: { elemMatch: { __component: "elements.events", title_contains: $searchkey } } }) {
data {
attributes {
effectiveDate
contentSections {
... on ComponentElementsEvents {
title
}
}
}
}
}
}
Here's what has been added to the query:
- A new filter for the
contentSectionsfield:contentSections: { elemMatch: { __component: "elements.events", title_contains: $searchkey } }. - The
elemMatchoperator is used to match components within the dynamic zone. We're specifying the component we want to filter by using the__componentfield with the valueelements.events. - The
title_containsoperator is used to filter the title field based on the$searchkeyvariable.
An alternative option would be to use the approach described here:
Let me know how it goes!
Thank you for sharing your thoughts with me. I have also attempted to follow that approach, but unfortunately, it did not work as expected.
Indeed that seems to be a bit more complex than I initially thought. It might be best to start a discussion on the official Strapi forum instead where there will be more Strapi experts:
https://forum.strapi.io/
1


