mutatingPostResolve Hook Configuration
Published: 2022-05-23
Mutating postResolve Hooks let you rewrite the response of an Operation. You've got access to the Request Context as well as the input, so it's possible to use these as a means to manipulating the Response of an Operation.
Here's an example:
const wunderGraphHooks = ConfigureWunderGraphHooks({queries: {Missions: {async mutatingPostResolve(ctx: Context, input: MissionsInput, response: MissionsResponse) {return {...response,data: {...response.data,missions: response.data?.missions?.filter(mission => mission.id === "Telstar")}}}}},})
In this case, we're filtering the missions of the Response Object using a static name. However, it's also possible to create one or more hook Variables so that you can dynamically manipulate the response based on the user inputs. These inputs can be validated using the JSON Schema directive.