From 35b0c5b09e0b92acca3d4b33c439f5a8ba5fa782 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 16 Jan 2024 12:41:18 +0100 Subject: [PATCH] Avoid fetching from database when only accessing primary key --- src/index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 6e07a2a..c7d1edc 100644 --- a/src/index.js +++ b/src/index.js @@ -156,10 +156,15 @@ module.exports = { types: { [typeName]: async function({ id }) { let normalFields = fieldNames.map((name) => { - return [ name, async (_, { dlayerKnexTable }) => { - let record = await dlayerKnexTable.load(id); - return record[name]; - }]; + if (name === primaryKey) { + // No point making a trip to the database if all we need is an ID we already have + return [ name, id ]; + } else { + return [ name, async (_, { dlayerKnexTable }) => { + let record = await dlayerKnexTable.load(id); + return record[name]; + }]; + } }); let relationFields = [];