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

Merged
merged 13 commits into from
May 30, 2024

Conversation

xcfox
Copy link
Contributor

@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 (ff00035).
Report is 28 commits behind head on master.

Files Patch % Lines
...corators/createResolverClassMiddlewareDecorator.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      328      -36     
==========================================
+ Hits         1764     1787      +23     
- Misses         83       86       +3     

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

@MichalLytek
Copy link
Owner

@xcfox

  • I've renamed the custom middleware decorator functions
  • added changelog entries
  • updated docs to reflect the changes
  • adjusted examples to use resolver class scoped middleware
  • added missing shim export for this new decorator function

Now it should be ready to be merged!
Thanks for your contribution! ❤️

@MichalLytek MichalLytek linked an issue May 30, 2024 that may be closed by this pull request
@MichalLytek MichalLytek merged commit 405fe77 into MichalLytek:master May 30, 2024
5 of 7 checks passed
MichalLytek added a commit that referenced this pull request May 30, 2024
* ✨feat(Authorized): decorator to resolver

* ✨feat(decorators/UseMiddleware): middleware decorator to Resolver

* ✨feat(decorators): createMiddlewareDecorator

* 🐞fix(tests/middlewares): add @resolver() to LocalResolver

* 🐞fix(decorators): export createMiddlewareDecorator

* Rename create decorator method

* Add changelog

* Add comments for cleanup in tests

* Add missing shim for createClassMiddlewareDecorator

* Update docs to reflect new class-defined auth and middlewares

* Rename createResolverClassMiddlewareDecorator and update changelog

* Update examples to use resolver class scoped middleware

---------

Co-authored-by: Michał Lytek <michal.wojciech.lytek@gmail.com>
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.

Custom Decorator to a class Resolver
3 participants