Skip to content

fix: Extract request object from correct execution context#1403

Open
benkenawell wants to merge 2 commits into
open-feature:mainfrom
benkenawell:bugfix/req-flags-enabled
Open

fix: Extract request object from correct execution context#1403
benkenawell wants to merge 2 commits into
open-feature:mainfrom
benkenawell:bugfix/req-flags-enabled

Conversation

@benkenawell

Copy link
Copy Markdown

This PR

Adds graphql context support to the RequireFlagsEnabled decorator in the nestjs package.

Unfortunately, if using the decorator on a graphql resolver or operation, the request object lives in a slightly different place. This PR adds @nestjs/graphql as an optional peer dependency and adds support in the RequireFlagsEnabled decorator for the graphql context. If you aren't using nestjs/graphql, this should still work fine. If you are, it should also transparently work.

Related Issues

Fixes #1369

Notes

Follow-up Tasks

There may be other places where the request object is accessed, those should also use the getRequestFromContext helper function I've added to the utils.ts file.

How to test

npm ci and npm test should cover it.

@benkenawell benkenawell requested review from a team as code owners May 5, 2026 01:02
@benkenawell benkenawell changed the title Extract request object from correct execution context fix: Extract request object from correct execution context May 5, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces optional GraphQL support to the NestJS package. The primary changes involve adding @nestjs/graphql as an optional peer dependency and implementing a new utility function, getRequestFromContext, which allows decorators to retrieve the request object from both HTTP and GraphQL execution contexts. Feedback was provided regarding a potential runtime error in this utility function; specifically, the implementation needs more robust optional chaining to safely handle environments where the optional GraphQL dependency is not installed.

Comment thread packages/nest/src/utils.ts Outdated
@benkenawell benkenawell force-pushed the bugfix/req-flags-enabled branch from cf4455e to b3916a2 Compare May 5, 2026 11:14
Unfortunately, if using the decorator on a graphql resolver or operation, the request object lives in a slightly different place.  This commit adds @nestjs/graphql as an optional peer dependency and adds support in the RequireFlagsEnabled decorator for the graphql context.

Signed-off-by: Benjamin Kenawell <ben@withremark.com>
@benkenawell benkenawell force-pushed the bugfix/req-flags-enabled branch from b3916a2 to 79bccc5 Compare May 5, 2026 11:15
import type { Client, EvaluationContext } from '@openfeature/server-sdk';
import { OpenFeature } from '@openfeature/server-sdk';
import { ExecutionContext } from '@nestjs/common';
import type { GqlExecutionContext as GqlExecutionContextClass, GqlContextType } from '@nestjs/graphql';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just importing the types but could this be an issue if @nestjs/graphql isn't available?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created this branch as a little sample repository for showing that this shouldn't be an issue if @nestjs/graphql isn't available. I have my changes vendored in, so you could clone and install and try it if you want. (I realize that sounds pretty insecure. Changing the install path for those packages to this PR would also work. I only vendored them in to make it easier to share)

@beeme1mr beeme1mr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @benkenawell. It looks good from what I can tell. Would it be possible to add a test?

@benkenawell

Copy link
Copy Markdown
Author

yes! It might be a few days before I get the chance, but I would be happy to write a few tests. I believe the types won't be an issue, but I will double check that by importing into a new nest project as well!

@benkenawell

Copy link
Copy Markdown
Author

@beeme1mr I added some unit tests for the getRequestFromContext function. I think I may need to add a graphql driver like apollo to add proper integration tests for graphql. If you're okay with that, I'm happy to write those tests as well.

Signed-off-by: Benjamin Kenawell <ben@withremark.com>
@benkenawell benkenawell force-pushed the bugfix/req-flags-enabled branch from c72f571 to 903a553 Compare May 14, 2026 20:15
benkenawell added a commit to benkenawell/nestjs-openfeature-testing that referenced this pull request May 14, 2026

@beeme1mr beeme1mr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks @benkenawell

@lukas-reining would you mind taking a look when you have a moment?

@lukas-reining

Copy link
Copy Markdown
Member

Sorry for the delay, I am on a conference right now and will try to make sure to look latest next week @beeme1mr @benkenawell!

let GqlExecutionContext: typeof GqlExecutionContextClass | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/consistent-type-imports
({ GqlExecutionContext } = require('@nestjs/graphql') as typeof import('@nestjs/graphql'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this won't ESM build; we'll get "Dynamic require not supported" in true ESM and the try/catch silently swallows it. Could you switch to await import('@nestjs/graphql')? intercept() is already async so it should be pretty straightforward unless I'm missing something.

@toddbaert toddbaert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and sorry for the delayed review. I think you're right.

Unfortunately this is a blocker I think. If you disagree, let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] NestJS Graphql RequireFlagsEnabled Decorator Support

4 participants