Skip to content

perf(graphql): cache internal core module lookup#3821

Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom
darek-phorest:perf/cache-internal-core-module-lookup
Feb 4, 2026
Merged

perf(graphql): cache internal core module lookup#3821
kamilmysliwiec merged 1 commit intonestjs:masterfrom
darek-phorest:perf/cache-internal-core-module-lookup

Conversation

@darek-phorest
Copy link
Copy Markdown
Contributor

@darek-phorest darek-phorest commented Feb 3, 2026

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

The registerContextProvider method in ResolversExplorerService iterates through all modules on every request-scoped resolver call to find InternalCoreModule. In applications with many modules (100+), this becomes a significant performance bottleneck with O(n) complexity on every call.

Issue Number: #3816

What is the new behavior?

Cache the InternalCoreModule reference after the first lookup, reducing complexity from O(n) to O(1) for subsequent calls. Uses lazy initialization so projects without request-scoped resolvers pay no cost.

Also added test coverage for @Inject(CONTEXT) to verify REQUEST token injection works correctly.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Test plan:

  • Existing e2e tests pass (request-scoped.spec.ts, graphql-request-scoped.spec.ts)
  • Added new test to verify @Inject(CONTEXT) receives proper context with req defined
  • All Apollo, GraphQL, and Mercurius e2e tests pass

Cache the InternalCoreModule reference after the first lookup to avoid
iterating through all modules on every request-scoped resolver call.

This reduces complexity from O(n) to O(1) for subsequent calls, providing
significant performance improvements in applications with many modules.

Fixes nestjs#3816
@kamilmysliwiec kamilmysliwiec merged commit 0f8f2a5 into nestjs:master Feb 4, 2026
1 check passed
@kamilmysliwiec
Copy link
Copy Markdown
Member

LGTM

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants