graphile / graphile-engine

Monorepo home of graphile-build, graphile-build-pg, graphile-utils, postgraphile-core and graphql-parse-resolve-info. Build a high-performance easily-extensible GraphQL schema by combining plugins!
https://www.graphile.org/
762 stars 129 forks source link

No Way to Access Request From Resolvers? #799

Closed machineghost closed 2 years ago

machineghost commented 2 years ago

Summary

It can be useful to access the Request object for a request, for instance to access the session property of it (when using express-sessions). Normally the request is provided as part of the context argument to a resolver ... but makeExtendSchemaPlugin resolver functions get passed a context argument with no request property.

Steps to reproduce

  1. Extend a postgraphile setup with makeExtendSchemaPlugin
  2. Define a new resolver (eg. on Query or Mutation)
  3. Look at the third (ie. context) argument: no request

Expected results

It should be possible to access the request

Actual results

It's not

Additional context

"postgraphile": "^4.10.0",

Possible Solution

It would seem trivial to just expose this, but I haven't looked at the source yet. If someone would point me in the right direction I'd be happy to submit a PR.

benjie commented 2 years ago

It’s unwise to add the request directly to the context because it only makes sense when using HTTP requests (e.g. it doesn’t make so much sense when using websockets, or in schema only usage). Instead I recommend you add accessor methods to the context that state what you wish to do rather than how you wish to do it (e.g. “logout()” rather than “req.session = null”).

Either way, you can use the additionalGraphQLContextFromRequest callback to add whatever things to context you need 👍