Skip to content

Commit

Permalink
rename compose function and use operation to detect create/update mod…
Browse files Browse the repository at this point in the history
…e. (#2837)
  • Loading branch information
gautamsi authored May 5, 2020
1 parent e0e3e30 commit c3270e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/beige-dragons-jump.md
Original file line number Diff line number Diff line change
@@ -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`.
11 changes: 6 additions & 5 deletions packages/list-plugins/lib/tracking/atTracking.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -29,17 +29,18 @@ 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;
}
if (updated) {
resolvedData[updatedAtField] = dateNow;
}
} else {
}
if (operation === 'update') {
// update mode

// if no data received from the mutation, skip the update
Expand All @@ -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 };
};

Expand Down
8 changes: 4 additions & 4 deletions packages/list-plugins/lib/tracking/byTracking.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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;
Expand All @@ -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 };
};

Expand Down
2 changes: 1 addition & 1 deletion packages/list-plugins/lib/utils.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit c3270e5

Please sign in to comment.