Request hooks

Request hooks are hooks provided by ky. These hooks let you intercept request and response stage of the query or mutation and modify basically everything.

beforeRequest

This hook allows you to modify a request before it's sent to the server. It's commonly useful for adding an authorization token for your server or attaching a CSRF token.

1 import {Draqula} from 'draqula';
2
3 const authToken = 'super secret token';
4
5 const client = new Draqula('https://graph.ql', {
6 hooks: {
7 beforeRequest: [
8 request => {
9 request.headers.set('authorization', `Bearer ${authToken}`);
10 }
11 ]
12 }
13});

afterResponse

This hook lets you check or modify a response before Draqula starts processing it. You could use afterResponse hooks to log requests, report errors to Sentry (or other error tracking service) or reauthenticate a user with a fresh access token.

1 import {Draqula} from 'draqula';
2
3 const client = new Draqula('https://graph.ql', {
4 hooks: {
5 afterResponse: [
6 response => {
7 if (response.status === 500) {
8 alert('Server is down, check back later');
9 }
10 }
11 ]
12 }
13});

Check out more examples in the ky docs.