Examples

Inject Bearer Token Example

The inject bearer token example demonstrates how you can use hooks to inject a token into origin requests.

Configuration

WunderGraph allows you to write http transport hooks, so you're able to manipulate requests before they are sent to the server, and responses before they are returned to the resolvers.

Hooks have access to the user object, so you can inject metadata from the user into a request, like an ID token in this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// wundergraph.server.ts
export default configureWunderGraphServer<HooksConfig, InternalClient>(() => ({
hooks: {
global: {
httpTransport: {
onOriginRequest: {
enableForAllOperations: true,
hook: async ({ request, user }) => {
if (user && user.rawIdToken) {
request.headers.set('Authorization', `Bearer ${user.rawIdToken}`)
}
return request
},
},
},
},
queries: {},
mutations: {},
},
graphqlServers: [],
}))

If you want to learn more about http transport hooks, have a look at the onOriginRequest hook reference.

Was this article helpful to you?
Provide feedback

Edit this page