Optimize GraphQL query with Bookshelf

This commit is contained in:
Aurelsicoko 2018-04-02 19:24:36 +02:00
parent 6c2399715a
commit e917c61b02
5 changed files with 26 additions and 20 deletions

View File

@ -55,4 +55,4 @@
"npm": ">= 5.3.0" "npm": ">= 5.3.0"
}, },
"license": "MIT" "license": "MIT"
} }

View File

@ -46,4 +46,4 @@
"npm": ">= 5.0.0" "npm": ">= 5.0.0"
}, },
"license": "MIT" "license": "MIT"
} }

View File

@ -3,7 +3,7 @@ const _ = require('lodash');
module.exports = { module.exports = {
find: async function (params) { find: async function (params) {
return this return this
.find(params.query) .find(params.where)
.limit(Number(params.limit)) .limit(Number(params.limit))
.sort(params.sort) .sort(params.sort)
.skip(Number(params.skip)); .skip(Number(params.skip));

View File

@ -15,7 +15,7 @@ module.exports = {
limit, limit,
skip, skip,
sort, sort,
query: request, where: request,
queryAttribute queryAttribute
}); });
}, },

View File

@ -158,10 +158,13 @@ module.exports = {
// or the shadow CRUD resolver (aka Content-Manager). // or the shadow CRUD resolver (aka Content-Manager).
const resolver = (() => { const resolver = (() => {
if (isSingular) { if (isSingular) {
return return _.get(handler, `Query.${pluralize.singular(name)}.resolver`,
_.get(handler, `Query.${pluralize.singular(name)}.resolver`, async () => {
resolvers.fetch({ ...params, id: options.id }, queryOpts) 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`, const resolver = _.get(handler, `Query.${pluralize.plural(name)}.resolver`,
@ -173,7 +176,9 @@ module.exports = {
convertedParams.skip = convertedParams.start; convertedParams.skip = convertedParams.start;
convertedParams.query = where.where; 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; return acc;
}, initialState); }, initialState);
// console.log(name);
// console.log(model.associations);
// console.log();
// Add parameters to optimize association query. // Add parameters to optimize association query.
(model.associations || []) (model.associations || [])
.filter(association => association.type === 'collection') .filter(association => association.type === 'collection')
@ -308,7 +309,7 @@ module.exports = {
// TODO: // TODO:
// - Handle limit, skip, etc options // - Handle limit, skip, etc options
_.merge(acc.resolver[globalId], { _.merge(acc.resolver[globalId], {
[association.alias]: (obj, options, context) => { [association.alias]: async (obj, options, context) => {
// Construct parameters object to retrieve the correct related entries. // Construct parameters object to retrieve the correct related entries.
const params = { const params = {
model: association.model || association.collection, model: association.model || association.collection,
@ -335,21 +336,26 @@ module.exports = {
const convertedParams = strapi.utils.models.convertParams(name, this.convertToParams(options)); const convertedParams = strapi.utils.models.convertParams(name, this.convertToParams(options));
const where = strapi.utils.models.convertParams(name, options.where || {}); const where = strapi.utils.models.convertParams(name, options.where || {});
// Limit, order, etc.
Object.assign(queryOpts, convertedParams); 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 // Construct the "where" query to only retrieve entries which are
// related to this entry. // related to this entry.
[association.via]: obj[ref.primaryKey], [association.via]: obj[ref.primaryKey],
...where.where ...where.where
}; }).where;
queryOpts.skip = convertedParams.start;
} }
return association.model ? const value = await (association.model ?
resolvers.fetch(params, association.plugin): resolvers.fetch(params, association.plugin):
resolvers.fetchAll(params, queryOpts) resolvers.fetchAll(params, queryOpts));
return value.toJSON ? value.toJSON() : value;
} }
}); });
}); });