Skip to Content

customResolve Hook Configuration

Published: 2022-05-24

The customResolve hook runs right before the actual GraphQL Operation is executed. The hook can be used to "break" the request chain and immediately return a response.

You can either return data or an errors object, e.g. to indicate that the request arguments are invalid.

Good use cases for this hook are custom input validation or authorization checks, as it's possible to immediately return a response without proceeding with the request chain.

If you return "void" (i.e. no return value), the request chain will continue. If you return an object, the request chain will be interrupted and the object will be returned to the client.

Here's an example:

const wunderGraphHooks = configureWunderGraphHooksWithClient(client => ({
mutations: {
UpdateProfile: {
customResolve: async (ctx, input) => {
if (input.token === undefined){
return {
errors: [
{
message: "Token is required",
path: ["token"]
}
]
}
}
}
}
},
}));

This example checks for the existence of a token in the input object and returns an error if it's missing. Otherwise, the request chain continues and the GraphQL Operation is executed.

Hooks of the kind customResolve will always contain a Context (ctx) Object. The Context object contains useful information, e.g. the User Object if the user is authenticated.

If there are any Variables defined for the preResolve Hook, these will be available as the second parameter. The structure of the Hooks is generated with TypeScript types. This means, all parameters of the Hook will be typed.


Product

Comparisons

Subscribe to our newsletter!

Stay informed when great things happen! Get the latest news about APIs, GraphQL and more straight into your mailbox.

© 2022 WunderGraph