hayes / pothos

Pothos GraphQL is library for creating GraphQL schemas in typescript using a strongly typed code first approach
https://pothos-graphql.dev
ISC License
2.34k stars 162 forks source link

Customize 'extensions' property in response #1119

Closed michaelbridge closed 8 months ago

michaelbridge commented 10 months ago

The Pothos plugins framework provides tools to customize individual resolution of fields (i.e. wrapResolvers). Is there a mechanism for customizing the 'extensions' property in the response with metadata that pertains to the entirety of the request?

hayes commented 10 months ago

Unfortunately that isn't something that Pothos can do. Pothos "just" generated a GraphQL schema that can be plugged into any server or other library capable of executing queries against a schema. This means that everything Pothos does it does during the execution of resolvers, but Pothos doesn't have any way to hook into the final generated response.

Generally the best way to do something like that is to store metadata using the context object, and then adding something that copies the data from the context object onto the response object before it is returned.

This simplest version of this would be just adding an extensions object to the context object, and then just copying that over either via something like an envelope plugin, or just as part of formatting the response depending on how your app is set up. A lot of plugins use symbols or weak maps to store metadata on the context object (eg caches for auth checks, or dataloaders).

Hope that helps. Always curious about what kinds of custom plugins people are building, if you're open to sharing