diff --git a/packages/strapi-hook-bookshelf/lib/query-utils.js b/packages/strapi-hook-bookshelf/lib/query-utils.js index 16ea8018ec..25d5d41b06 100644 --- a/packages/strapi-hook-bookshelf/lib/query-utils.js +++ b/packages/strapi-hook-bookshelf/lib/query-utils.js @@ -14,9 +14,23 @@ class QueryBuilder { let fieldKey = `${model.collectionName}.${attributeKey}`; if (association) { if (association.nature === 'manyToMany') { - const { attribute, column } = model.attributes[key]; + const { attribute, column } = model.attributes[association.via]; fieldKey = `${association.tableCollectionName}.${attribute}_${column}`; } + + /** + * If for instance we want to filter on the model relation, such as + * { + * product: { + * method: 'whereIn', + * value: ["1", "2", "3"] + * } + * } + * the fieldKey should be "products.id" and not "products.product" + */ + if (attributeKey === key) { + fieldKey = `${model.collectionName}.${model.primaryKey}`; + } } if (value.symbol) { @@ -108,7 +122,7 @@ class QueryBuilder { qb.innerJoin( relationTable, - `${association.tableCollectionName}.${strapiModel.attributes[key].attribute}_${strapiModel.attributes[key].column}`, + `${association.tableCollectionName}.${strapiModel.attributes[association.alias].attribute}_${strapiModel.attributes[association.alias].column}`, `${relationTable}.${astModel.primaryKey}`, ); } else { diff --git a/packages/strapi-plugin-graphql/services/Loaders.js b/packages/strapi-plugin-graphql/services/Loaders.js index 6c7376c5d4..787319bef7 100644 --- a/packages/strapi-plugin-graphql/services/Loaders.js +++ b/packages/strapi-plugin-graphql/services/Loaders.js @@ -97,7 +97,26 @@ module.exports = { return data .filter(entry => { - const entryValue = entry[ids.alias][astModel.primaryKey]; + /** + * In some cases (Mongoose for instance) the entry will be populated so we need to access its value using the primary key + * Example + * // Mongoose + * entry = { + * product: { + * _id: "some_id", // the value is entry["product"]["_id"] + * price: 10 + * }, + * company: "strapi" + * } + * // Bookshelf + * entry = { + * product: 1, // the value is entry["product"] + * company: "strapi" + * } + */ + const entryValue = _.isUndefined(entry[ids.alias][astModel.primaryKey]) + ? entry[ids.alias] + : entry[ids.alias][astModel.primaryKey]; return entryValue.toString() === ids.value.toString(); }) .slice(skip, skip + limit);