Skip to content

Commit

Permalink
Use AuthedRelationship to power byTracking (keystonejs#2811)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesstelford authored Apr 23, 2020
1 parent 5958d58 commit 003c604
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-baboons-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/list-plugins': minor
---

Leverage AuthedRelationship field within the byTracking plugin
42 changes: 13 additions & 29 deletions packages/list-plugins/lib/tracking/byTracking.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Relationship } = require('@keystonejs/fields');
const { AuthedRelationship } = require('@keystonejs/fields-authed-relationship');
const { composeResolveInput } = require('../utils');

const _byTracking = ({ created = true, updated = true }) => ({
Expand All @@ -7,13 +7,8 @@ const _byTracking = ({ created = true, updated = true }) => ({
...byFieldOptions
} = {}) => ({ fields = {}, hooks = {}, ...rest }) => {
const relationshipOptions = {
type: Relationship,
type: AuthedRelationship,
ref: 'User',
access: {
read: true,
create: false,
update: false,
},
...byFieldOptions,
};

Expand All @@ -30,30 +25,19 @@ const _byTracking = ({ created = true, updated = true }) => ({
}

const newResolveInput = ({ resolvedData, existingItem, originalInput, context }) => {
const { authedItem: { id = null } = {} } = context;
if (existingItem === undefined) {
// create mode
if (created) {
resolvedData[createdByField] = id;
}
if (updated) {
resolvedData[updatedByField] = id;
}
} else {
// update mode

if (
// if no data received from the mutation, skip the update
if (Object.keys(originalInput).length === 0) {
return resolvedData;
}

if (created) {
delete resolvedData[createdByField]; // createdByField No longer sent by api/admin, but access control can be skipped!
}
if (updated) {
resolvedData[updatedByField] = id;
}
Object.keys(originalInput).length === 0 &&
// opted-in to updatedBy tracking
updated &&
// this is an update
existingItem !== undefined
) {
// If not logged in, the id is set to `null`
const { authedItem: { id = null } = {} } = context;
resolvedData[updatedByField] = id;
}

return resolvedData;
};
const originalResolveInput = hooks.resolveInput;
Expand Down
3 changes: 2 additions & 1 deletion packages/list-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"node": ">=10.0.0"
},
"dependencies": {
"@keystonejs/fields": "^9.0.0"
"@keystonejs/fields": "^9.0.0",
"@keystonejs/fields-authed-relationship": "^1.0.2"
},
"repository": "https://github.com/keystonejs/keystone/tree/master/packages/list-plugins"
}

0 comments on commit 003c604

Please sign in to comment.