mirror of
https://github.com/strapi/strapi.git
synced 2025-10-16 10:33:34 +00:00
Optimize GraphQL query with Bookshelf
This commit is contained in:
parent
6c2399715a
commit
e917c61b02
@ -55,4 +55,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
@ -46,4 +46,4 @@
|
||||
"npm": ">= 5.0.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ const _ = require('lodash');
|
||||
module.exports = {
|
||||
find: async function (params) {
|
||||
return this
|
||||
.find(params.query)
|
||||
.find(params.where)
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.skip));
|
||||
|
@ -15,7 +15,7 @@ module.exports = {
|
||||
limit,
|
||||
skip,
|
||||
sort,
|
||||
query: request,
|
||||
where: request,
|
||||
queryAttribute
|
||||
});
|
||||
},
|
||||
|
@ -158,10 +158,13 @@ module.exports = {
|
||||
// or the shadow CRUD resolver (aka Content-Manager).
|
||||
const resolver = (() => {
|
||||
if (isSingular) {
|
||||
return
|
||||
_.get(handler, `Query.${pluralize.singular(name)}.resolver`,
|
||||
resolvers.fetch({ ...params, id: options.id }, queryOpts)
|
||||
);
|
||||
return _.get(handler, `Query.${pluralize.singular(name)}.resolver`,
|
||||
async () => {
|
||||
const value = await resolvers.fetch({ ...params, id: options.id }, queryOpts);
|
||||
|
||||
return value.toJSON ? value.toJSON() : value;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const resolver = _.get(handler, `Query.${pluralize.plural(name)}.resolver`,
|
||||
@ -173,7 +176,9 @@ module.exports = {
|
||||
convertedParams.skip = convertedParams.start;
|
||||
convertedParams.query = where.where;
|
||||
|
||||
return resolvers.fetchAll(params, {...queryOpts, ...convertedParams});
|
||||
const value = await resolvers.fetchAll(params, {...queryOpts, ...convertedParams});
|
||||
|
||||
return value.toJSON ? value.toJSON() : value;
|
||||
}
|
||||
);
|
||||
|
||||
@ -250,10 +255,6 @@ module.exports = {
|
||||
return acc;
|
||||
}, initialState);
|
||||
|
||||
// console.log(name);
|
||||
// console.log(model.associations);
|
||||
// console.log();
|
||||
|
||||
// Add parameters to optimize association query.
|
||||
(model.associations || [])
|
||||
.filter(association => association.type === 'collection')
|
||||
@ -308,7 +309,7 @@ module.exports = {
|
||||
// TODO:
|
||||
// - Handle limit, skip, etc options
|
||||
_.merge(acc.resolver[globalId], {
|
||||
[association.alias]: (obj, options, context) => {
|
||||
[association.alias]: async (obj, options, context) => {
|
||||
// Construct parameters object to retrieve the correct related entries.
|
||||
const params = {
|
||||
model: association.model || association.collection,
|
||||
@ -335,21 +336,26 @@ module.exports = {
|
||||
const convertedParams = strapi.utils.models.convertParams(name, this.convertToParams(options));
|
||||
const where = strapi.utils.models.convertParams(name, options.where || {});
|
||||
|
||||
// Limit, order, etc.
|
||||
Object.assign(queryOpts, convertedParams);
|
||||
|
||||
queryOpts.query = {
|
||||
// Skip.
|
||||
queryOpts.skip = convertedParams.start;
|
||||
|
||||
// Where.
|
||||
queryOpts.query = strapi.utils.models.convertParams(name, {
|
||||
// Construct the "where" query to only retrieve entries which are
|
||||
// related to this entry.
|
||||
[association.via]: obj[ref.primaryKey],
|
||||
...where.where
|
||||
};
|
||||
|
||||
queryOpts.skip = convertedParams.start;
|
||||
}).where;
|
||||
}
|
||||
|
||||
return association.model ?
|
||||
const value = await (association.model ?
|
||||
resolvers.fetch(params, association.plugin):
|
||||
resolvers.fetchAll(params, queryOpts)
|
||||
resolvers.fetchAll(params, queryOpts));
|
||||
|
||||
return value.toJSON ? value.toJSON() : value;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user