Skip to content
KeystoneJS LogoKeystoneJSv5

Hooks

The APIs for each hook are mostly the same across the 3 hook types (list hooks, field hooks, field type hooks).

Any differences are called out in the documentation below.

Usage

keystone.createList('User', {
  fields: {
    name: {
      type: Text
      hooks: { /* hooks config */ },
    },
  },
  hooks: { /* hooks config */ },
  ...
});

Config

optionTypeDescription
resolveInputFunctionExecuted after access control checks and default values are assigned.
validateInputFunctionExecuted after resolveInput. Should throw if resolvedData is invalid.
beforeChangeFunctionExecuted after validateInput.
afterChangeFunctionExecuted once the mutation has been completed and all transactions finalised.
validateDeleteFunctionExecuted after access control checks. Should throw if resolvedData is invalid.
beforeDeleteFunctionExecuted after validateDelete.
afterDeleteFunctionExecuted once the delete mutation has been completed and all transactions finalised.

resolveInput

Executed after access control checks and default values are assigned. Used to modify the resolvedData.

The result of resolveInput can be a Promise or an Object. It should have the same structure as the resolvedData.

The result is passed to the next function in the execution order.

Note: resolveInput is not executed for deleted items.

Usage:

const resolveInput = ({
  resolvedData,
  existingItem,
  originalInput,
  context,
  list
}) => resolvedData;

validateInput

Executed after resolveInput. Should throw if resolvedData is invalid.

Usage

const validateInput = ({
  resolvedData,
  existingItem,
  originalInput,
  addFieldValidationError,
  context,
  list,
}) => {
  /* throw any errors here */
};

Note: validateInput is not executed for deleted items. See: validateDelete

beforeChange

Executed after validateInput. beforeChange is not used to manipulate data but can be used to preform actions before data is saved.

Note: beforeChange is not executed for deleted items. See: beforeDelete

afterChange

Executed once the mutation has been completed and all transactions finalised.

Usage

const afterChange = ({
  updatedItem,
  existingItem,
  originalInput,
  context,
  list
}) => {
  /* side effects here */
};

Note: afterChange is not executed for deleted items. See: afterDelete

validateDelete

Executed after access control checks. Should throw if delete operation is invalid.

Usage

const validateDelete = ({
  existingItem,
  addFieldValidationError,
  context,
  list,
}) => {
  /* throw any errors here */
};

beforeDelete

Executed after validateDelete.

Usage

const beforeDelete = ({
  existingItem,
  context,
  list,
}) => {
  /* throw any errors here */
};

afterDelete

Executed once the delete mutation has been completed and all transactions finalised.

Usage

const afterDelete = ({
  existingItem,
  context,
  list,
}) => {
  /* side effects here */
};

Hooks function properties

ParameterTypeDescription
resolvedDataObjectAn object containing data received by the graphQL mutation and defaults values.
existingItemanyThe current stored value. (undefined for create)
originalInputObjectAn object containing arguments passed to the field in the graphQL query.
contextApollo ContextThe Apollo context object for this request.
listObjectAn Object providing access to List functions. List properties.
addFieldValidationErrorFunctionUsed to set a field validation error. Accepts a String.

List properties

The list property contain an object providing access to List functions:

{
  /**
   * @param args Object The same arguments as the *WhereUniqueInput graphql
   * type
   * @param context Object The Apollo context object for this request
   * @param options.skipAccessControl Boolean By default access control _of
   * the user making the initial request_ is still tested. Disable all
   * Access Control checks with this flag
   *
   * @return Promise<Object> The found item
   */
  query: (args, context, options) => Promise<Object>,

  /**
   * @param args Object The same arguments as the *WhereInput graphql type
   * @param context Object The Apollo context object for this request
   * @param options.skipAccessControl Boolean By default access control _of
   * the user making the initial request_ is still tested. Disable all
   * Access Control checks with this flag
   *
   * @return Promise<[Object]|[]> The found item. May reject with Access
   * Control errors.
   */
  queryMany: (args, context, options) => Promise<Object|null>,

  /**
   * @param args Object The same arguments as the *WhereInput graphql type
   * @param context Object The Apollo context object for this request
   * @param options.skipAccessControl Boolean By default access control _of
   * the user making the initial request_ is still tested. Disable all
   * Access Control checks with this flag
   *
   * @return Promise<Object> Meta data about the found items. Currently
   * contains only a single key: `count`.
   */
  queryManyMeta: (args, context, options) => Promise<Object>,

  /**
   * @param key String The string name of a Keystone list
   * @return Object The programatic API of the requested list.
   */
  getList: (key) => Object,
}

Have you found a mistake, something that is missing, or could be improved on this page? Please edit the Markdown file on GitHub and submit a PR with your changes.

Edit Page