Skip to content

Commit

Permalink
Fix nested relational limit for m2o fetching (#12235)
Browse files Browse the repository at this point in the history
Fixes #12134
  • Loading branch information
rijkvanzanten committed Mar 18, 2022
1 parent 4b6fa29 commit 2bb350f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions api/src/database/run-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default async function runAST(
const node = merge({}, nestedNode, {
query: { limit: env.RELATIONAL_BATCH_SIZE, offset: batchCount * env.RELATIONAL_BATCH_SIZE },
});

nestedItems = (await runAST(node, schema, { knex, nested: true })) as Item[] | null;

if (nestedItems) {
Expand All @@ -108,7 +109,11 @@ export default async function runAST(
batchCount++;
}
} else {
nestedItems = (await runAST(nestedNode, schema, { knex, nested: true })) as Item[] | null;
const node = merge({}, nestedNode, {
query: { limit: -1 },
});

nestedItems = (await runAST(node, schema, { knex, nested: true })) as Item[] | null;

if (nestedItems) {
// Merge all fetched nested records with the parent items
Expand Down Expand Up @@ -341,12 +346,13 @@ function mergeWithParentItems(
});

parentItem[nestedNode.fieldKey].push(...itemChildren);

if (nestedNode.query.offset && nestedNode.query.offset >= 0) {
parentItem[nestedNode.fieldKey] = parentItem[nestedNode.fieldKey].slice(nestedNode.query.offset);
}
if (nestedNode.query.limit && nestedNode.query.limit >= 0) {
parentItem[nestedNode.fieldKey] = parentItem[nestedNode.fieldKey].slice(0, nestedNode.query.limit ?? 100);
}

parentItem[nestedNode.fieldKey] = parentItem[nestedNode.fieldKey].slice(0, nestedNode.query.limit ?? 100);

parentItem[nestedNode.fieldKey] = parentItem[nestedNode.fieldKey].sort((a: Item, b: Item) => {
// This is pre-filled in get-ast-from-query
const sortField = nestedNode.query.sort![0]!;
Expand Down

0 comments on commit 2bb350f

Please sign in to comment.