Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Populate fragments for polymorphic relations #14879

Merged
merged 12 commits into from
Nov 29, 2022
Merged
24 changes: 21 additions & 3 deletions packages/core/database/lib/query/helpers/populate/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@ const morphToMany = async (input, ctx) => {
.where({
[joinColumn.name]: referencedValues,
...(joinTable.on || {}),
// If the populateValue contains an "on" property,
// only populate the types defined in it
...('on' in populateValue
? { [morphColumn.typeColumn.name]: Object.keys(populateValue.on) }
: {}),
})
.orderBy([joinColumn.name, 'order'])
.execute({ mapResults: false });
Expand All @@ -470,6 +475,8 @@ const morphToMany = async (input, ctx) => {
}, {});

const map = {};
const { on, ...typePopulate } = populateValue;

for (const type of Object.keys(idsByType)) {
const ids = idsByType[type];

Expand All @@ -483,7 +490,7 @@ const morphToMany = async (input, ctx) => {
const qb = db.entityManager.createQueryBuilder(type);

const rows = await qb
.init(populateValue)
.init(on && type in on ? on[type] : typePopulate)
Convly marked this conversation as resolved.
Show resolved Hide resolved
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
.where({ [idColumn.referencedColumn]: ids })
.execute({ mapResults: false });
Expand Down Expand Up @@ -540,6 +547,8 @@ const morphToOne = async (input, ctx) => {
}, {});

const map = {};
const { on, ...typePopulate } = populateValue;

for (const type of Object.keys(idsByType)) {
const ids = idsByType[type];

Expand All @@ -552,7 +561,7 @@ const morphToOne = async (input, ctx) => {
const qb = db.entityManager.createQueryBuilder(type);

const rows = await qb
.init(populateValue)
.init(on && type in on ? on[type] : typePopulate)
Convly marked this conversation as resolved.
Show resolved Hide resolved
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
.where({ [idColumn.referencedColumn]: ids })
.execute({ mapResults: false });
Expand All @@ -579,7 +588,16 @@ const morphToOne = async (input, ctx) => {

// TODO: Omit limit & offset to avoid needing a query per result to avoid making too many queries
const pickPopulateParams = (populate) => {
const fieldsToPick = ['select', 'count', 'where', 'populate', 'orderBy', 'filters', 'ordering'];
const fieldsToPick = [
'select',
'count',
'where',
'populate',
'orderBy',
'filters',
'ordering',
'on',
];

if (populate.count !== true) {
fieldsToPick.push('limit', 'offset');
Expand Down
26 changes: 21 additions & 5 deletions packages/core/utils/lib/convert-query-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
* Converts the standard Strapi REST query params to a more usable format for querying
* You can read more here: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#filters
*/

const {
isNil,
toNumber,
isInteger,
has,
isEmpty,
isObject,
isPlainObject,
cloneDeep,
get,
mergeAll,
isNil,
toNumber,
isInteger,
} = require('lodash/fp');
const _ = require('lodash');
const parseType = require('./parse-type');
Expand Down Expand Up @@ -185,8 +186,23 @@ const convertPopulateObject = (populate, schema) => {
return acc;
}

// FIXME: This is a temporary solution for dynamic zones that should be
// fixed when we'll implement a more accurate way to query them
if (typeof subPopulate === 'object' && 'on' in subPopulate) {
Convly marked this conversation as resolved.
Show resolved Hide resolved
return {
...acc,
[key]: {
on: Object.entries(subPopulate.on).reduce(
(acc, [type, typeSubPopulate]) => ({
...acc,
[type]: convertNestedPopulate(typeSubPopulate, strapi.getModel(type)),
}),
{}
),
},
};
}

// TODO: Deprecated way of handling dynamic zone populate queries. It's kept as is,
// as removing it could break existing user queries but should be removed in V5.
if (attribute.type === 'dynamiczone') {
const populates = attribute.components
.map((uid) => strapi.getModel(uid))
Expand Down