From c3270e50a11ee328a31c4a8d8e77ee7ef873881c Mon Sep 17 00:00:00 2001 From: Gautam Singh <5769869+gautamsi@users.noreply.github.com> Date: Tue, 5 May 2020 05:42:55 +0530 Subject: [PATCH] rename compose function and use operation to detect create/update mode. (#2837) --- .changeset/beige-dragons-jump.md | 7 +++++++ packages/list-plugins/lib/tracking/atTracking.js | 11 ++++++----- packages/list-plugins/lib/tracking/byTracking.js | 8 ++++---- packages/list-plugins/lib/utils.js | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 .changeset/beige-dragons-jump.md diff --git a/.changeset/beige-dragons-jump.md b/.changeset/beige-dragons-jump.md new file mode 100644 index 00000000000..7df11bd3089 --- /dev/null +++ b/.changeset/beige-dragons-jump.md @@ -0,0 +1,7 @@ +--- +'@keystonejs/list-plugins': patch +--- + +Tweaked hooks and utility function. +* Renamed `composeResolveInput` utility function to `composeHook` to indicate right use by name, this can also be used in other hook type and not just `resolveInput` hook. +* Switch to use of `operation` param to hook for detecting if this is `create` or `update` operation instead of existingItem being `undefined`. \ No newline at end of file diff --git a/packages/list-plugins/lib/tracking/atTracking.js b/packages/list-plugins/lib/tracking/atTracking.js index df54eaa7a12..f54bfa5b608 100644 --- a/packages/list-plugins/lib/tracking/atTracking.js +++ b/packages/list-plugins/lib/tracking/atTracking.js @@ -1,5 +1,5 @@ const { DateTime } = require('@keystonejs/fields'); -const { composeResolveInput } = require('../utils'); +const { composeHook } = require('../utils'); const _atTracking = ({ created = true, updated = true }) => ({ updatedAtField = 'updatedAt', @@ -29,9 +29,9 @@ const _atTracking = ({ created = true, updated = true }) => ({ }; } - const newResolveInput = ({ resolvedData, existingItem, originalInput }) => { + const newResolveInput = ({ resolvedData, operation, originalInput }) => { const dateNow = new Date().toISOString(); - if (existingItem === undefined) { + if (operation === 'create') { // create mode if (created) { resolvedData[createdAtField] = dateNow; @@ -39,7 +39,8 @@ const _atTracking = ({ created = true, updated = true }) => ({ if (updated) { resolvedData[updatedAtField] = dateNow; } - } else { + } + if (operation === 'update') { // update mode // if no data received from the mutation, skip the update @@ -57,7 +58,7 @@ const _atTracking = ({ created = true, updated = true }) => ({ return resolvedData; }; const originalResolveInput = hooks.resolveInput; - hooks.resolveInput = composeResolveInput(originalResolveInput, newResolveInput); + hooks.resolveInput = composeHook(originalResolveInput, newResolveInput); return { fields, hooks, ...rest }; }; diff --git a/packages/list-plugins/lib/tracking/byTracking.js b/packages/list-plugins/lib/tracking/byTracking.js index 30bb54e5e11..dbcd7e8fe96 100644 --- a/packages/list-plugins/lib/tracking/byTracking.js +++ b/packages/list-plugins/lib/tracking/byTracking.js @@ -1,5 +1,5 @@ const { AuthedRelationship } = require('@keystonejs/fields-authed-relationship'); -const { composeResolveInput } = require('../utils'); +const { composeHook } = require('../utils'); const _byTracking = ({ created = true, updated = true }) => ({ updatedByField = 'updatedBy', @@ -24,14 +24,14 @@ const _byTracking = ({ created = true, updated = true }) => ({ }; } - const newResolveInput = ({ resolvedData, existingItem, originalInput, context }) => { + const newResolveInput = ({ resolvedData, operation, originalInput, context }) => { if ( // if no data received from the mutation, skip the update Object.keys(originalInput).length === 0 && // opted-in to updatedBy tracking updated && // this is an update - existingItem !== undefined + operation === 'update' ) { // If not logged in, the id is set to `null` const { authedItem: { id = null } = {} } = context; @@ -41,7 +41,7 @@ const _byTracking = ({ created = true, updated = true }) => ({ return resolvedData; }; const originalResolveInput = hooks.resolveInput; - hooks.resolveInput = composeResolveInput(originalResolveInput, newResolveInput); + hooks.resolveInput = composeHook(originalResolveInput, newResolveInput); return { fields, hooks, ...rest }; }; diff --git a/packages/list-plugins/lib/utils.js b/packages/list-plugins/lib/utils.js index 89c252d2771..f27958d8f4a 100644 --- a/packages/list-plugins/lib/utils.js +++ b/packages/list-plugins/lib/utils.js @@ -1,4 +1,4 @@ -exports.composeResolveInput = (originalHook, newHook) => async params => { +exports.composeHook = (originalHook, newHook) => async params => { let { resolvedData } = params; if (originalHook) { resolvedData = await originalHook(params);