mirror of
https://github.com/strapi/strapi.git
synced 2025-09-02 13:23:12 +00:00
Simplify GraphQL resolver and adapt the ContentManager to the new changes
This commit is contained in:
parent
17d8eb36c0
commit
2bdbf12b0a
@ -8,18 +8,14 @@ const _ = require('lodash');
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
fetchAll: async (params, query) => {
|
fetchAll: async (params, query) => {
|
||||||
const { limit, skip, sort, query : request, queryAttribute, source, populate = [] } = query;
|
const { query: request, source, populate = [], ...filters } = query;
|
||||||
const filters = strapi.utils.models.convertParams(params.model, query);
|
const queryFilter = !_.isEmpty(request) ? {
|
||||||
const { where = {} } = !_.isEmpty(request) ? strapi.utils.models.convertParams(params.model, request) : filters;
|
...filters, // Filters is an object containing the limit/sort and start
|
||||||
|
...request
|
||||||
|
} : filters;
|
||||||
|
|
||||||
// Find entries using `queries` system
|
// Find entries using `queries` system
|
||||||
return await strapi.query(params.model, source).find({
|
return await strapi.query(params.model, source).find(queryFilter, populate);
|
||||||
limit: limit || filters.limit,
|
|
||||||
skip: skip || filters.start || 0,
|
|
||||||
sort: sort || filters.sort,
|
|
||||||
where,
|
|
||||||
queryAttribute,
|
|
||||||
}, populate);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
search: async (params, query) => {
|
search: async (params, query) => {
|
||||||
@ -42,10 +38,9 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
count: async (params, query) => {
|
count: async (params, query) => {
|
||||||
const { source } = query;
|
const { source, ...filters } = query;
|
||||||
const filters = strapi.utils.models.convertParams(params.model, 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) => {
|
fetch: async (params, source, populate, raw = true) => {
|
||||||
|
@ -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.
|
* Security to avoid infinite limit.
|
||||||
*
|
*
|
||||||
@ -261,9 +278,7 @@ module.exports = {
|
|||||||
query: {
|
query: {
|
||||||
value: {
|
value: {
|
||||||
...this.convertToParams(_.omit(_options, 'where'), model.primaryKey),
|
...this.convertToParams(_.omit(_options, 'where'), model.primaryKey),
|
||||||
..._options.where,
|
...this.convertToQuery(_options.where),
|
||||||
// Avoid population.
|
|
||||||
_populate: model.associations.filter(a => !a.dominant && _.isEmpty(a.model)).map(a => a.alias),
|
|
||||||
},
|
},
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
|
@ -359,7 +359,7 @@ module.exports = {
|
|||||||
model: association.model || association.collection,
|
model: association.model || association.collection,
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryOpts = {
|
let queryOpts = {
|
||||||
source: association.plugin,
|
source: association.plugin,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -371,56 +371,16 @@ module.exports = {
|
|||||||
if (association.type === 'model') {
|
if (association.type === 'model') {
|
||||||
params[ref.primaryKey] = _.get(obj, [association.alias, ref.primaryKey], obj[association.alias]);
|
params[ref.primaryKey] = _.get(obj, [association.alias, ref.primaryKey], obj[association.alias]);
|
||||||
} else {
|
} else {
|
||||||
// Apply optional arguments to make more precise nested request.
|
const queryParams = Query.amountLimiting(options);
|
||||||
const convertedParams = strapi.utils.models.convertParams(
|
queryOpts = {
|
||||||
name,
|
...queryOpts,
|
||||||
Query.convertToParams(Query.amountLimiting(options), ref.primaryKey),
|
...Query.convertToParams(_.omit(queryParams, 'where')), // Convert filters (sort, limit and start/skip)
|
||||||
);
|
...Query.convertToQuery(queryParams.where)
|
||||||
|
};
|
||||||
|
|
||||||
const where = strapi.utils.models.convertParams(
|
// Construct the "where" query to only retrieve entries which are
|
||||||
name,
|
// related to this entry.
|
||||||
options.where || {},
|
_.set(queryOpts, ['query', association.via], obj[ref.primaryKey]);
|
||||||
);
|
|
||||||
|
|
||||||
// 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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const loaderName = association.plugin ? `${association.plugin}__${params.model}`: params.model;
|
const loaderName = association.plugin ? `${association.plugin}__${params.model}`: params.model;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user