Skip to content

Commit

Permalink
List plugin improvement (#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamsi authored May 22, 2020
1 parent ccb2372 commit cbfc674
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/gentle-deers-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@keystonejs/keystone': minor
---

Added additional parameters to list plugins which expose `{ listKey, keystone }` to plugins. This helps plugin know name of list and keystone instance. Existing plugins are not affected by this change.

New plugin signature: `(config, { listKey, keystone }) => config`
3 changes: 2 additions & 1 deletion docs/api/create-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ Changes the path in the Admin UI. Updating `plural` and `singular` values will n

### `plugins`

An array of functions that modify config values. Plugin functions receive a config object and can modify or extend this. They should return a valid list config.
An array of functions that modify config values. Plugin functions receive `(config, { listKey, keystone })`, where `config` is the a list config object, `listKey` is the name of the list, and `keystone` is the keystone object. They should return a valid list config. Plugin functions are executed in the order provided in the list, with the output config of one being passed as input to the next. The output of the final plugin is used to construct the `List` instance.


```javascript
const setupUserList = ({ fields, ...config }) => {
Expand Down
42 changes: 24 additions & 18 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
flatten,
unique,
filterValues,
compose,
} = require('@keystonejs/utils');
const {
validateFieldAccessControl,
Expand Down Expand Up @@ -306,23 +305,30 @@ module.exports = class Keystone {
throw new Error(`Invalid list name "${key}". List names cannot start with an underscore.`);
}

const list = new List(key, compose(config.plugins || [])(config), {
getListByKey,
queryHelper: this._buildQueryHelper.bind(this),
adapter: adapters[adapterName],
defaultAccess: this.defaultAccess,
registerType: type => this.registeredTypes.add(type),
isAuxList,
createAuxList: (auxKey, auxConfig) => {
if (isAuxList) {
throw new Error(
`Aux list "${key}" shouldn't be creating more aux lists ("${auxKey}"). Something's probably not right here.`
);
}
return this.createList(auxKey, auxConfig, { isAuxList: true });
},
schemaNames: this._schemaNames,
});
// composePlugins([f, g, h])(o, e) = h(g(f(o, e), e), e)
const composePlugins = fns => (o, e) => fns.reduce((acc, fn) => fn(acc, e), o);

const list = new List(
key,
composePlugins(config.plugins || [])(config, { listKey: key, keystone: this }),
{
getListByKey,
queryHelper: this._buildQueryHelper.bind(this),
adapter: adapters[adapterName],
defaultAccess: this.defaultAccess,
registerType: type => this.registeredTypes.add(type),
isAuxList,
createAuxList: (auxKey, auxConfig) => {
if (isAuxList) {
throw new Error(
`Aux list "${key}" shouldn't be creating more aux lists ("${auxKey}"). Something's probably not right here.`
);
}
return this.createList(auxKey, auxConfig, { isAuxList: true });
},
schemaNames: this._schemaNames,
}
);
this.lists[key] = list;
this.listsArray.push(list);
this._listCRUDProvider.lists.push(list);
Expand Down

0 comments on commit cbfc674

Please sign in to comment.