Enable filtering on nested components

This commit is contained in:
Convly 2022-06-16 10:21:03 +02:00
parent 632672a95c
commit 34bb591cea
2 changed files with 30 additions and 2 deletions

View File

@ -14,7 +14,7 @@ module.exports = ({ strapi }) => {
const extension = strapi.plugin('graphql').service('extension');
const { getFiltersInputTypeName, getScalarFilterInputTypeName } = utils.naming;
const { isStrapiScalar, isRelation } = utils.attributes;
const { isStrapiScalar, isRelation, isComponent } = utils.attributes;
const { attributes } = contentType;
@ -50,6 +50,8 @@ module.exports = ({ strapi }) => {
// Handle relations
else if (isRelation(attribute)) {
addRelationalAttribute(t, attributeName, attribute);
} else if (isComponent(attribute)) {
addComponentAttribute(t, attributeName, attribute);
}
}
@ -87,6 +89,22 @@ module.exports = ({ strapi }) => {
builder.field(attributeName, { type: getFiltersInputTypeName(model) });
};
const addComponentAttribute = (builder, attributeName, attribute) => {
const utils = strapi.plugin('graphql').service('utils');
const extension = strapi.plugin('graphql').service('extension');
const { getFiltersInputTypeName } = utils.naming;
const component = strapi.getModel(attribute.component);
// If there is no component corresponding to the attribute configuration, then ignore it
if (!component) return;
// If the component is disabled, then ignore it too
if (extension.shadowCRUD(component.uid).isDisabled()) return;
builder.field(attributeName, { type: getFiltersInputTypeName(component) });
};
return {
buildContentTypeFilters,
};

View File

@ -42,7 +42,7 @@ module.exports = ({ strapi }) => {
* @return {object | object[]}
*/
graphQLFiltersToStrapiQuery(filters, contentType = {}) {
const { isStrapiScalar, isMedia, isRelation } = getService('utils').attributes;
const { isStrapiScalar, isMedia, isRelation, isComponent } = getService('utils').attributes;
const { operators } = getService('builders').filters;
const ROOT_LEVEL_OPERATORS = [operators.and, operators.or, operators.not];
@ -86,6 +86,16 @@ module.exports = ({ strapi }) => {
// and update the value within `resultMap`
resultMap[key] = this.graphQLFiltersToStrapiQuery(value, relModel);
}
// If it's a deep filter on a component
else if (isComponent(attribute)) {
// Fetch the model from the component attribute
const componentModel = strapi.getModel(attribute.component);
// Recursively apply the mapping to the value using the fetched model,
// and update the value within `resultMap`
resultMap[key] = this.graphQLFiltersToStrapiQuery(value, componentModel);
}
}
// Handle the case where the key is not an attribute (operator, ...)