Fix 500 on singleton retrieval

Fixes #418
This commit is contained in:
rijkvanzanten
2020-09-24 14:10:20 -04:00
parent e7489abfa6
commit 2af75e3cfb
7 changed files with 25 additions and 13 deletions

View File

@@ -13,7 +13,7 @@ type RunASTOptions = {
child?: boolean;
};
export default async function runAST(originalAST: AST, options?: RunASTOptions): Promise<Item | Item[]> {
export default async function runAST(originalAST: AST, options?: RunASTOptions): Promise<null | Item | Item[]> {
const ast = cloneDeep(originalAST);
const query = options?.query || ast.query;
@@ -27,6 +27,8 @@ export default async function runAST(originalAST: AST, options?: RunASTOptions):
const rawItems: Item | Item[] = await dbQuery;
if (!rawItems || (Array.isArray(rawItems) && rawItems.length === 0)) return null;
// Run the items through the special transforms
const payloadService = new PayloadService(ast.name, { knex });
let items = await payloadService.processValues('read', rawItems);
@@ -48,8 +50,10 @@ export default async function runAST(originalAST: AST, options?: RunASTOptions):
let nestedItems = await runAST(nestedAST, { knex, child: true });
// Merge all fetched nested records with the parent items
items = mergeWithParentItems(nestedItems, items, nestedAST, tempLimit);
if (nestedItems) {
// Merge all fetched nested records with the parent items
items = mergeWithParentItems(nestedItems, items, nestedAST, tempLimit);
}
}
// During the fetching of data, we have to inject a couple of required fields for the child nesting