Skip to content

feat(graphql): add field middleware decorator for fields and resolvers - #4193

Merged
kamilmysliwiec merged 1 commit into
masterfrom
feat/field-middleware-decorator
Oct 26, 2026
Merged

kamilmysliwiec merged 1 commit into
masterfrom
feat/field-middleware-decorator

Conversation

@kamilmysliwiec

Copy link
Copy Markdown
Member

PR Checklist

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

  • Feature

What is the current behavior?

Field middleware can only be passed through the middleware option of @Field() or @ResolveField(), so it can't be applied by a separate (e.g., composed) decorator such as @Auth(Role.Admin).

Issue Number: #1446

What is the new behavior?

A new @UseFieldMiddleware(...middleware) decorator applies field middleware to @Field() properties and @ResolveField() methods. It can be placed above or below either decorator and composes with applyDecorators():

export function Auth(...roles: Role[]) {
  return applyDecorators(
    Extensions({ roles }),
    UseFieldMiddleware(checkRoleMiddleware),
  );
}

@ObjectType()
export class User {
  @Field()
  @Auth(Role.Admin)
  email: string;
}

Middleware runs in this order: global buildSchemaOptions.fieldMiddleware, then the middleware option of @Field()/@ResolveField(), then @UseFieldMiddleware() middleware in argument order (stacked decorators run lowest-first, as with @UseGuards()). For field resolvers, field middleware runs before the enhancers enabled through fieldResolverEnhancers. The middleware is kept on fields copied by mapped types (PartialType(), etc.), and the model shim gets a no-op version of the decorator.

The middleware is stored under its own metadata key and merged when the schema is built, so the decorator order never matters (@ResolveField() always writes its own middleware metadata). Field resolvers with @UseFieldMiddleware() also skip the fast field-resolver path, like those with the middleware option. In schema-first applications, the decorator works on @ResolveField() methods, like the middleware option does.

Does this PR introduce a breaking change?

  • Yes
  • No

Closes #1446

@kamilmysliwiec
kamilmysliwiec merged commit 9b0c3b2 into master Oct 26, 2026
1 check passed
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.

Applying Field Metadata Outside Of @Field() Decorator

1 participant