Wrap @Directive(...) into a helper decorator #1360
Answered
by
MichalLytek
carlocorradini
asked this question in
Q&A
-
It is possible to create a wrapper around Use Case: @auth(
roles: [String!]! = [],
permissions: [String!]! = [],
) Using @Resolver(Example)
class ExampleResolver() {
@Mutation(() => Example)
@Directive(`@auth`)
secureMutation() { // ... }
@Query(() => Example)
@Directive(`@auth(roles: ["ADMIN"], permissions: ["example:secureQuery"])`)
secureQuery() { // ... }
} It could be better if it's possible to create a wrapper around @Resolver(Example)
class ExampleResolver() {
@Mutation(() => Example)
@Auth
secureMutation() { // ... }
@Query(() => Example)
@Auth({ permissions: ["example:secureQuery"] })
secureQuery() { // ... }
} Where the type AuthArgs = {
roles?: Roles[];
permissions?: Permissions[];
};
function Auth(args?: AuthArgs) {
const roles: Roles[] = args.roles ?? [Roles.ADMIN];
const permissions: Permissions[] = args?.permissions ?? [];
const rolesString = roles
.map((role) => `"${role}"`)
.join(',');
const permissionsString = permissions
.map((permission) => `"${permission}"`)
.join(',');
return Directive(`@auth(roles: [${ rolesString }], permissions: [${ permissionsString }])`);
} Is it possible? Thanks 🥳 |
Beta Was this translation helpful? Give feedback.
Answered by
MichalLytek
Oct 7, 2022
Replies: 1 comment 3 replies
-
decorator is a function, you can see some examples of aliases for other decorators as well |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
carlocorradini
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
decorator is a function, you can see some examples of aliases for other decorators as well