2021-08-24 12:10:47 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
module.exports = ({ strapi }) => {
|
2021-09-22 14:39:36 +02:00
|
|
|
const { service: getGraphQLService } = strapi.plugin('graphql');
|
|
|
|
|
|
|
|
const { isMorphRelation, isMedia } = getGraphQLService('utils').attributes;
|
|
|
|
const { transformArgs } = getGraphQLService('builders').utils;
|
|
|
|
const { toEntityResponse, toEntityResponseCollection } = getGraphQLService('format').returnTypes;
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
return {
|
2021-09-22 14:39:36 +02:00
|
|
|
buildAssociationResolver({ contentTypeUID, attributeName }) {
|
2021-08-24 17:56:44 +02:00
|
|
|
const contentType = strapi.getModel(contentTypeUID);
|
|
|
|
const attribute = contentType.attributes[attributeName];
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
if (!attribute) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to build an association resolver for ${contentTypeUID}::${attributeName}`
|
|
|
|
);
|
|
|
|
}
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
const isMediaAttribute = isMedia(attribute);
|
|
|
|
const isMorphAttribute = isMorphRelation(attribute);
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
const targetUID = isMediaAttribute ? 'plugins::upload.file' : attribute.target;
|
|
|
|
const isToMany = isMediaAttribute ? attribute.multiple : attribute.relation.endsWith('Many');
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
const targetContentType = strapi.getModel(targetUID);
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-09-01 12:06:51 +02:00
|
|
|
return async (parent, args = {}) => {
|
2021-08-24 17:56:44 +02:00
|
|
|
const transformedArgs = transformArgs(args, {
|
|
|
|
contentType: targetContentType,
|
|
|
|
usePagination: true,
|
|
|
|
});
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-09-10 10:24:33 +02:00
|
|
|
const data = await strapi.entityService.load(
|
2021-08-24 17:56:44 +02:00
|
|
|
contentTypeUID,
|
2021-09-01 12:06:51 +02:00
|
|
|
parent,
|
2021-08-24 17:56:44 +02:00
|
|
|
attributeName,
|
2021-09-10 10:24:33 +02:00
|
|
|
transformedArgs
|
2021-08-24 17:56:44 +02:00
|
|
|
);
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-09-22 14:39:36 +02:00
|
|
|
const info = {
|
|
|
|
args: transformedArgs,
|
|
|
|
resourceUID: targetUID,
|
|
|
|
};
|
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
// If this a polymorphic association, it returns the raw data
|
|
|
|
if (isMorphAttribute) {
|
|
|
|
return data;
|
|
|
|
}
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
// If this is a to-many relation, it returns an object that
|
|
|
|
// matches what the entity-response-collection's resolvers expect
|
|
|
|
else if (isToMany) {
|
2021-09-22 14:39:36 +02:00
|
|
|
return toEntityResponseCollection(data, info);
|
2021-08-24 17:56:44 +02:00
|
|
|
}
|
2021-08-24 12:10:47 +02:00
|
|
|
|
2021-08-24 17:56:44 +02:00
|
|
|
// Else, it returns an object that matches
|
|
|
|
// what the entity-response's resolvers expect
|
2021-09-22 14:39:36 +02:00
|
|
|
return toEntityResponse(data, info);
|
2021-08-24 17:56:44 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|