fix(@nestjs/graphql): preserve ResolveField options for all overloads#3939
Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom Apr 20, 2026
Merged
Conversation
The argument destructuring in the @ResolveField decorator only branched on whether the
first argument was a function. The else arm assumed the first argument was a string
property name and that the second argument was a typeFunc, so calls of the form
@ResolveField({ middleware: [...] }) put the options object in the name slot and left
the options slot undefined. Calls of the form @ResolveField('aliased', { middleware: [...] })
similarly routed the options object into the typeFunc slot and dropped middleware. The
only consistently-working shape was @ResolveField(typeFunc, { middleware }), which is why
adding an explicit return type appeared to fix the bug.
Extend the destructuring to detect (a) options-only calls when the first argument is a
plain object and (b) propertyName-with-options calls when the second argument is not a
function. The typeFunc-first overloads keep their existing behaviour.
Closes nestjs#2587
Member
|
lgtm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
(Marking only Bugfix:)
What is the current behavior?
Issue Number: #2587
Field middleware configured via
@ResolveField({ middleware: [...] })or@ResolveField('aliased', { middleware: [...] })is silently dropped. The same middleware fires correctly for plain@Field({ middleware: [...] })and for@ResolveField(() => SomeType, { middleware: [...] }). The reporter and a co-reporter on the original issue identified that adding an explicitreturnTypeargument to@ResolveFieldmakes the middleware work — that observation is the fingerprint of the underlying decorator-argument-destructuring bug, not a middleware-wiring problem.What is the new behavior?
packages/graphql/lib/decorators/resolve-field.decorator.tsnow branches on the actual shape of the arguments rather than only onisFunction(firstArg). New routing:(a) first arg function → typeFunc-first overload (unchanged);
(b) first arg string + second arg function → propertyName + typeFunc + options (unchanged);
(c) first arg string + second arg object → propertyName + no-typeFunc + options;
(d) first arg object → no-name + no-typeFunc + options.
Adds an
isStringimport alongside the existingisFunction/isObjectfrom@nestjs/common/utils/shared.utils.A regression spec at
packages/graphql/tests/schema-builder/factories/resolve-field-middleware.spec.tsreadsFIELD_RESOLVER_MIDDLEWARE_METADATA,RESOLVER_NAME_METADATA, andRESOLVER_PROPERTY_METADATAstraight off the decorated method for three cases (typeFunc + options, options only, propertyName + options) — reading metadata directly avoids requiring an apollo/mercurius runtime to assert the wiring.Does this PR introduce a breaking change?
The change only adds correct handling for the previously-broken overloads. Calls that already worked (typeFunc-first or propertyName + typeFunc + options) continue to produce identical metadata.
Other information
Verified with
yarn test:e2e:graphql(89 passed plus the unrelated pre-existing Windows CRLF snapshot failures intests/plugin/model-class-visitor.spec.tses5-eager-importsandtests/plugin/readonly-visitor.spec.ts, both reproducible on clean master).npx oxlintclean on touched files. Prettier formatting applied with the project's bundled v3 binary (the repo'snpx prettiershim resolves to v2 and would reformat unrelated lines).