Hooks allow you to define a URL to send a POST request to when mutation on some type is performed. This lets you react to user actions and integrate third-party services without much hassle.
Hooks are stored in type ReindexHook
. It has the following fields:
type
- type, from operation on which hook triggers. If null, hook will
trigger to any type operation.trigger
- event that triggers the hook. Can be afterCreate
, afterUpdate
or afterDelete
.url
- full URL to send the request tofragment
- fragment body on the corresponding type payload. Must be
surrounded by {} and not have a name. Can include typed inline fragments.logLevel
- all
, error
or none
. Defines which results of the hook
fragment execution to save in ReindexHookLog
. all
- all results are saved,
error
- only failed hooks are saved.For example, let’s assume we have a service listening at
http://www.example.com/testservice
and we want to POST to it when new todo is
created.
First, we need to find the ID of Todo
ReindexType
.
{
reindexTypeByName(name: "Todo") {
id,
}
}
After that we can create a hook. Fragment is used on the same type as the mutation result (payload). Let’s say we want to get todo’s text and the user id that it belongs to.
mutation {
createReindexHook(input: {
type: "YOUR-TODO-TYPE-ID",
trigger: afterCreate,
url: "http://www.example.com/testservice",
fragment: "{ changedTodo { text, user { id } } }",
logLevel: error
}) {
id
}
}
Now every time new Todo will be created, the service should receive a POST request.
Operations on many-to-many relationships trigger hooks for both of the affected nodes.
Often it’s useful to check if your hook is actually doing something. It’s easy to make mistake in a fragment or URL. Hook logs provide a way to log executed hooks and then check if there was a problem.
Depending on your hook logLevel
, hook log will contain a list of hook log
results. You can access it directly by reading the corresponding hook:
{
reindexHookById(id: "YOUR-HOOK-ID") {
log(orderBy: CREATED_AT_DESC) {
nodes {
createdAt,
type,
errors
}
}
}
}
Zapier provides out of the box integrations with many services. This guide shows how to send emails through Mailgun by using Zapier Post-hook app.