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: Middleware Decorator to Resolver #1297

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

xcfox
Copy link

@xcfox xcfox commented Jun 16, 2022

This PR implements #620 and a part of #200
This PR enhances @UseMiddleware() and @Authorized(), now we can put them directly on the resolver so that we can avoid repeating on each method.

Use @UseMiddleware() like so:

@UseMiddleware(resolverMiddleware1)
@UseMiddleware(resolverMiddleware2, resolverMiddleware3)
@Resolver()
class SampleResolver {
  @Query()
  @UseMiddleware(middleware1)
  @UseMiddleware(middleware2)
  normalQuery(): boolean {
    middlewareLogs.push('normalQuery')
    return true
  }
}

Middlewares will be called in the order of global, resolver, field.

Use @Authorized() like so:

@Authorized()
@Resolver()
class SampleResolver {
  @Query()
  normalQuery(): boolean {
    return true
  }
  @Authorized('ADMIN')
  @Query()
  authQuery(): boolean {
    return true
  }
}

When you set roles for both the field and the resolver, the roles will be overwritten by the field's @Authorized()

I also make a function createMiddlewareDecorator to help you create custom middlewares, it is similar to createMethodDecorator, but can be placed on resolver

@MichalLytek MichalLytek self-requested a review June 21, 2022 09:17
@MichalLytek MichalLytek added Enhancement 🆕 New feature or request Community 👨‍👧 Something initiated by a community labels Jun 21, 2022
@jbmikk
Copy link

jbmikk commented Oct 17, 2022

@xcfox does this work with @Directive decorators too? That would be very nice.

Copy link

codecov bot commented May 7, 2024

Codecov Report

Attention: Patch coverage is 92.59259% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 95.40%. Comparing base (be33fd4) to head (09ff7ca).
Report is 27 commits behind head on master.

Files Patch % Lines
src/decorators/createClassMiddlewareDecorator.ts 66.66% 2 Missing ⚠️
src/decorators/Authorized.ts 87.50% 1 Missing ⚠️
src/decorators/UseMiddleware.ts 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1297      +/-   ##
==========================================
- Coverage   95.50%   95.40%   -0.10%     
==========================================
  Files         113      114       +1     
  Lines        1847     1873      +26     
  Branches      364      364              
==========================================
+ Hits         1764     1787      +23     
- Misses         83       86       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants