Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow injection of context #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ahilke
Copy link

@ahilke ahilke commented May 7, 2024

Fixes #54 (and I believe #22).

Registers the context object within Nest's dependency-injection sub-tree, so that it can be injected with @Inject(CONTEXT) from @nestjs/graphql (@Inject(REQUEST) from @nestjs/core also works), following https://docs.nestjs.com/fundamentals/module-ref#registering-request-provider. To quote:

Manually generated context identifiers (with ContextIdFactory.create()) represent DI sub-trees in which REQUEST provider is undefined as they are not instantiated and managed by the Nest dependency injection system.

To register a custom REQUEST object for a manually created DI sub-tree, use the ModuleRef#registerRequestByContextId() method

This allows accessing the context object from anywhere that allows injection, not just in resolvers, for example to provide request-specific logs. Since @Inject(REQUEST) also works, this makes it possible to write functionality that works for both GraphQL and REST endpoints.

I confirmed the behaviour using my reproduction project: https://github.com/ahilke/typegraphql-nestjs-inject-context/

I believe this adds considerable value to the library, as otherwise one has to work around GraphQL specifics to access the context/request.

@MichalLytek MichalLytek self-requested a review May 8, 2024 05:53
@MichalLytek MichalLytek added the enhancement New feature or request label May 8, 2024
@anto4capmo
Copy link

@MichalLytek could you provide any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@Inject(CONTEXT) is undefined
3 participants