Improve retrieving of relationships with GraphQL

This commit is contained in:
Aurelsicoko 2018-04-17 17:29:24 +02:00
parent 2d1d642c32
commit 96934a23da

View File

@ -147,7 +147,7 @@ module.exports = {
// Add bracket or not. // Add bracket or not.
const globalId = definition.plugin ? const globalId = definition.plugin ?
strapi.plugins[definition.plugin].models[ref].globalId: strapi.plugins[definition.plugin].models[ref].globalId:
strapi.models[definition.plugin].globalId; strapi.models[ref].globalId;
const plural = !_.isEmpty(definition.collection); const plural = !_.isEmpty(definition.collection);
if (plural) { if (plural) {
@ -349,7 +349,7 @@ module.exports = {
return ctx.body; return ctx.body;
} }
return values.toJSON ? values.toJSON() : values; return values && values.toJSON ? values.toJSON() : values;
} }
return resolver.call(null, obj, options, context) return resolver.call(null, obj, options, context)
@ -478,6 +478,7 @@ module.exports = {
switch (association.nature) { switch (association.nature) {
case 'manyMorphToOne': case 'manyMorphToOne':
case 'manyMorphToMany': case 'manyMorphToMany':
case 'manyToManyMorph':
return _.merge(acc.resolver[globalId], { return _.merge(acc.resolver[globalId], {
[association.alias]: async (obj, options, context) => { [association.alias]: async (obj, options, context) => {
const [ withRelated, withoutRelated ] = await Promise.all([ const [ withRelated, withoutRelated ] = await Promise.all([
@ -491,11 +492,12 @@ module.exports = {
}, plugin, []) }, plugin, [])
]); ]);
const entry = withRelated.toJSON ? withRelated.toJSON() : withRelated; const entry = withRelated && withRelated.toJSON ? withRelated.toJSON() : withRelated;
entry[association.alias].map((entry, index) => { entry[association.alias].map((entry, index) => {
const type = withoutRelated[association.alias][index].kind || const type = _.get(withoutRelated, `${association.alias}.${index}.kind`) ||
_.upperFirst(_.camelCase(withoutRelated[association.alias][index][`${association.alias}_type`])); _.upperFirst(_.camelCase(_.get(withoutRelated, `${association.alias}.${index}.${association.alias}_type`))) ||
_.upperFirst(_.camelCase(association[association.type]));
entry._type = type; entry._type = type;
@ -543,20 +545,36 @@ module.exports = {
// Skip. // Skip.
queryOpts.skip = convertedParams.start; queryOpts.skip = convertedParams.start;
// Where. switch (association.nature) {
queryOpts.query = strapi.utils.models.convertParams(name, { case 'manyToMany':
// Construct the "where" query to only retrieve entries which are const arrayOfIds = obj[association.alias].map(related => {
// related to this entry. return related[ref.primaryKey] || related;
[association.via]: obj[ref.primaryKey], });
...where.where
}).where; // Where.
queryOpts.query = strapi.utils.models.convertParams(name, {
// Construct the "where" query to only retrieve entries which are
// related to this entry.
[ref.primaryKey]: arrayOfIds,
...where.where
}).where;
break;
default:
// 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
}).where;
}
} }
const value = await (association.model ? const value = await (association.model ?
resolvers.fetch(params, association.plugin, []): resolvers.fetch(params, association.plugin, []):
resolvers.fetchAll(params, { ...queryOpts, populate: [] })); resolvers.fetchAll(params, { ...queryOpts, populate: [] }));
return value.toJSON ? value.toJSON() : value; return value && value.toJSON ? value.toJSON() : value;
} }
}); });
}); });