diff --git a/packages/strapi-plugin-content-manager/services/ContentManager.js b/packages/strapi-plugin-content-manager/services/ContentManager.js index 60fd2c1934..724fd51ecb 100644 --- a/packages/strapi-plugin-content-manager/services/ContentManager.js +++ b/packages/strapi-plugin-content-manager/services/ContentManager.js @@ -8,18 +8,14 @@ const _ = require('lodash'); module.exports = { fetchAll: async (params, query) => { - const { limit, skip, sort, query : request, queryAttribute, source, populate = [] } = query; - const filters = strapi.utils.models.convertParams(params.model, query); - const { where = {} } = !_.isEmpty(request) ? strapi.utils.models.convertParams(params.model, request) : filters; + const { query: request, source, populate = [], ...filters } = query; + const queryFilter = !_.isEmpty(request) ? { + ...filters, // Filters is an object containing the limit/sort and start + ...request + } : filters; // Find entries using `queries` system - return await strapi.query(params.model, source).find({ - limit: limit || filters.limit, - skip: skip || filters.start || 0, - sort: sort || filters.sort, - where, - queryAttribute, - }, populate); + return await strapi.query(params.model, source).find(queryFilter, populate); }, search: async (params, query) => { @@ -42,10 +38,9 @@ module.exports = { }, count: async (params, query) => { - const { source } = query; - const filters = strapi.utils.models.convertParams(params.model, query); + const { source, ...filters } = query; - return await strapi.query(params.model, source).count({ where: filters.where }); + return await strapi.query(params.model, source).count(filters); }, fetch: async (params, source, populate, raw = true) => { diff --git a/packages/strapi-plugin-graphql/services/Query.js b/packages/strapi-plugin-graphql/services/Query.js index b8a94fdd22..eb926522dd 100644 --- a/packages/strapi-plugin-graphql/services/Query.js +++ b/packages/strapi-plugin-graphql/services/Query.js @@ -27,6 +27,23 @@ module.exports = { }, {}); }, + convertToQuery: function(params) { + const result = {}; + + _.forEach(params, (value, key) => { + if (_.isPlainObject(value)) { + const flatObject = this.convertToQuery(value); + _.forEach (flatObject, (_value, _key) => { + result[`${key}.${_key}`] = _value; + }); + } else { + result[key] = value; + } + }); + + return result; + }, + /** * Security to avoid infinite limit. * @@ -261,9 +278,7 @@ module.exports = { query: { value: { ...this.convertToParams(_.omit(_options, 'where'), model.primaryKey), - ..._options.where, - // Avoid population. - _populate: model.associations.filter(a => !a.dominant && _.isEmpty(a.model)).map(a => a.alias), + ...this.convertToQuery(_options.where), }, writable: true, configurable: true diff --git a/packages/strapi-plugin-graphql/services/Resolvers.js b/packages/strapi-plugin-graphql/services/Resolvers.js index a07b3e9d64..0be1ee9a1a 100644 --- a/packages/strapi-plugin-graphql/services/Resolvers.js +++ b/packages/strapi-plugin-graphql/services/Resolvers.js @@ -359,7 +359,7 @@ module.exports = { model: association.model || association.collection, }; - const queryOpts = { + let queryOpts = { source: association.plugin, }; @@ -371,56 +371,16 @@ module.exports = { if (association.type === 'model') { params[ref.primaryKey] = _.get(obj, [association.alias, ref.primaryKey], obj[association.alias]); } else { - // Apply optional arguments to make more precise nested request. - const convertedParams = strapi.utils.models.convertParams( - name, - Query.convertToParams(Query.amountLimiting(options), ref.primaryKey), - ); + const queryParams = Query.amountLimiting(options); + queryOpts = { + ...queryOpts, + ...Query.convertToParams(_.omit(queryParams, 'where')), // Convert filters (sort, limit and start/skip) + ...Query.convertToQuery(queryParams.where) + }; - const where = strapi.utils.models.convertParams( - name, - options.where || {}, - ); - - // Limit, order, etc. - Object.assign(queryOpts, convertedParams, { where: where.where }); - - // Skip. - queryOpts.skip = convertedParams.start; - - switch (association.nature) { - case "manyToMany": { - const arrayOfIds = (obj[association.alias] || []).map( - related => { - return related[ref.primaryKey] || related; - } - ); - - Object.assign(queryOpts, { - ...queryOpts, - query: { - [ref.primaryKey]: arrayOfIds - } - }); - - break; - } - default: - Object.assign(queryOpts, { - ...queryOpts, - query: { - [association.via]: obj[ref.primaryKey] - } - }); - } - } - - if (queryOpts.hasOwnProperty('query') && - queryOpts.query.hasOwnProperty('id') && - queryOpts.query.id.hasOwnProperty('value') && - Array.isArray(queryOpts.query.id.value) - ) { - queryOpts.query.id.symbol = 'IN'; + // Construct the "where" query to only retrieve entries which are + // related to this entry. + _.set(queryOpts, ['query', association.via], obj[ref.primaryKey]); } const loaderName = association.plugin ? `${association.plugin}__${params.model}`: params.model;