I'm looking for a way to have an optional filter on graphql persisted queries for content fragments. It seems like it was possible at one point to define a variable for the query and, if it had no value, the filter would be ignored and all results would be returned. This is exactly the functionality I'm looking for but it doesn't seem to work that way anymore. If I omit the variable now the query returns results where the filtered-on property is null.
GraphQL itself has REGEX queries which would solve the problem nicely but the AEM API for content fragments doesn't have that.
Does anyone know how to get this optional filter functionality?
https://experienceleague.adobe.com/en/docs/experience-manager-65/content/assets/extending/graphql-ap...
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Yes, it is possible to have an optional variable in a Content Fragment GraphQL query. You can achieve this by using a default value for the variable and modifying the filter condition accordingly.
query MyQuery($myFilter: String = "") {
contentFragment {
list(filter: {
myProperty: $myFilter != "" ? $myFilter : NOT_NULL
}) {
items {
myProperty
}
}
}
}
In this example, the $myFilter variable has a default value of an empty string. The filter condition checks if $myFilter is not empty, and if so, it filters the results based on the value of $myFilter. If $myFilter is empty, the filter condition becomes NOT_NULL, which returns all results.
By using this approach, you can achieve the optional filter functionality you're looking for.
Not sure if it's something with our configuration but I'm unable to use that syntax. I get an error at the ! in Graphiql. So it seems I'm limited to the syntax described here https://experienceleague.adobe.com/en/docs/experience-manager-65/content/assets/extending/graphql-ap... (although, there are some things there that don't work either).
For now, I got more info from the client and the property they're going to filter on will be required so I have been able to set the queries up like so:
query getAssets( $keyFilter: String = "" ){
assetList(
filter: {
key: {_expressions: { value: $keyFilter, _operator: CONTAINS, _ignoreCase: true }}}
) {
items {
_path
key
...
}
}
}
Since the field will always have some string value this works even if they were to omit the filter parameter.
In the future though it would be nice to be able to do this with nullable values as well. As in if a filter parameter is not provided then return all content whether the key value is null or a string. As mentioned this apparently used to be the behaviour, or maybe it's only for cloud, but it'd be nice to have either way.
In short, filter if the parameter is set, do not filter if it's not set.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies