Global Hooks

Globals feature the ability to define the following hooks:

Config

All Global Hook properties accept arrays of synchronous or asynchronous functions. Each Hook type receives specific arguments and has the ability to modify specific outputs.

globals/example-hooks.js

// Global config
module.exports = {
slug: 'header',
fields: [
{ name: 'title', type: 'text'},
]
hooks: {
beforeValidate: [(args) => {...}],
beforeChange: [(args) => {...}],
beforeRead: [(args) => {...}],
afterChange: [(args) => {...}],
afterRead: [(args) => {...}],
}
}

beforeValidate

Runs before the update operation. This hook allows you to add or format data before the incoming data is validated.

const beforeValidateHook = async ({
data, // incoming data to update or create with
req, // full express request
originalDoc, // original document
}) => {
return data; // Return data to update the document with
}

beforeChange

Immediately following validation, beforeChange hooks will run within the update operation. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.

const beforeChangeHook = async ({
data, // incoming data to update or create with
req, // full express request
originalDoc, // original document
}) => {
return data; // Return data to update the document with
}

afterChange

After a global is updated, the afterChange hook runs. Use this hook to purge caches of your applications, sync site data to CRMs, and more.

const afterChangeHook = async ({
doc, // full document data
req, // full express request
}) => {
return data;
}

beforeRead

Runs before findOne global operation is transformed for output by afterRead. This hook fires before hidden fields are removed and before localized fields are flattened into the requested locale. Using this Hook will provide you with all locales and all hidden fields via the doc argument.

const beforeReadHook = async ({
doc, // full document data
req, // full express request
}) => {...}

afterRead

Runs as the last step before a global is returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.

const afterReadHook = async ({
doc, // full document data
req, // full express request
}) => {...}
Next

GraphQL Overview