Examples

Hooks Example

The Hooks example demonstrates how you can write custom Middleware functions to "hook" into the request lifecycle.

Configuration

If you want to get the full picture of what's possible with hooks, please have a look into the wundergraph.server.ts reference.

In this case, we're keeping it simple and implement a mutatingPostResolve hook to inject a custom rocket into the response. This hook allows us to modify the response object before it is sent back to the client. We've got access to the client request, the response and the user, so we're pretty flexible in what we can do with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// wundergraph.server.ts
export default configureWunderGraphServer<HooksConfig, InternalClient>(() => ({
hooks: {
queries: {
Dragons: {
/**
* mutatingPostResolve is a hook that is called after the query has been resolved.
* It can be used to alter the response .
*/
mutatingPostResolve: async ({ user, response, clientRequest }) => {
console.log('mutatingPostResolve', clientRequest.requestURI)
return {
data: {
spacex_dragons: [
{
name: 'Custom Falcon 9',
active: true,
},
...response.data.spacex_dragons,
],
},
}
},
},
},
mutations: {},
},
graphqlServers: [],
}))

Was this article helpful to you?
Provide feedback

Edit this page