Edit page

Field resolution hooks

In many cases, it might be desired to perform some aditional action before or after some field is resolved. Most common use-case could be authorization of user.

Hooks are special kind of functions added to field with @Before or @After decorator.

@Before and @After hooks

Let's say we want to send informations about useage of some field of our graphql api to analitycs server. We could achieve that with:

Using resolver informations inside hook function

Arguments passed to resolver are exactly the same, as ones passed to native graphql resolver funtion. Signature of a @Before hook function is

for @After, it is:

In case, we'd like to add id of removed user to logs, we'd change our code to:

Notes

  • If one field has many hooks of the same type - they're executed in parallel.
  • Field resolver is not called until all @Before hooks are resolved