mirror of
https://github.com/strapi/strapi.git
synced 2025-11-04 11:54:10 +00:00
Handle polymorphic relations in assocs resolvers
This commit is contained in:
parent
e16b36ecb6
commit
0eeaa91c7a
@ -246,6 +246,8 @@ const addPolymorphicRelationalAttribute = options => {
|
|||||||
if (isToManyRelation) {
|
if (isToManyRelation) {
|
||||||
builder = builder.list;
|
builder = builder.list;
|
||||||
}
|
}
|
||||||
|
// todo[v4]: How to handle polymorphic relation w/ entity response collection types?
|
||||||
|
// -> Currently return raw polymorphic entities
|
||||||
|
|
||||||
const resolve = buildAssociationResolver({
|
const resolve = buildAssociationResolver({
|
||||||
contentTypeUID: contentType.uid,
|
contentTypeUID: contentType.uid,
|
||||||
|
|||||||
@ -17,8 +17,12 @@ const buildAssociationResolver = ({ contentTypeUID, attributeName, strapi }) =>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo[v4]: make sure polymorphic relations aren't breaking here
|
const isMedia = utils.isMedia(attribute);
|
||||||
const targetUID = utils.isMedia(attribute) ? 'plugins::upload.file' : attribute.target;
|
const isMorph = utils.isMorphRelation(attribute);
|
||||||
|
|
||||||
|
const targetUID = isMedia ? 'plugins::upload.file' : attribute.target;
|
||||||
|
const isToMany = isMedia ? attribute.multiple : attribute.relation.endsWith('Many');
|
||||||
|
|
||||||
const targetContentType = strapi.getModel(targetUID);
|
const targetContentType = strapi.getModel(targetUID);
|
||||||
|
|
||||||
return async (parent, args = {}) => {
|
return async (parent, args = {}) => {
|
||||||
@ -27,20 +31,30 @@ const buildAssociationResolver = ({ contentTypeUID, attributeName, strapi }) =>
|
|||||||
usePagination: true,
|
usePagination: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// todo[v4]: move the .load to the entity service?
|
// Since we're using the entity-manager & not the entity-service to load the
|
||||||
|
// association, we need to apply some transformation to the transformed args object
|
||||||
const entityManagerArgs = {
|
const entityManagerArgs = {
|
||||||
...omit(['start', 'filters'], transformedArgs),
|
...omit(['start', 'filters'], transformedArgs),
|
||||||
where: transformedArgs.filters,
|
where: transformedArgs.filters,
|
||||||
offset: transformedArgs.start,
|
offset: transformedArgs.start,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// todo[v4]: move the .load to the entity service so we can use the same args everywhere?
|
||||||
const data = await entityManager.load(contentTypeUID, parent, attributeName, entityManagerArgs);
|
const data = await entityManager.load(contentTypeUID, parent, attributeName, entityManagerArgs);
|
||||||
|
|
||||||
// todo[v4]: Replace with a check on the attribute (handle case where data is null but for an array
|
// If this a polymorphic association, it returns the raw data
|
||||||
if (Array.isArray(data)) {
|
if (isMorph) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is a to-many relation, it returns an object that
|
||||||
|
// matches what the entity-response-collection's resolvers expect
|
||||||
|
else if (isToMany) {
|
||||||
return { nodes: data, info: { args: transformedArgs, resourceUID: targetUID } };
|
return { nodes: data, info: { args: transformedArgs, resourceUID: targetUID } };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Else, it returns an object that matches
|
||||||
|
// what the entity-response's resolvers expect
|
||||||
return { value: data };
|
return { value: data };
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user