MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
https://typegraphql.com
MIT License
8.03k stars 675 forks source link

Field authorization defined in the Input Type field #1723

Open bkostrowiecki opened 2 months ago

bkostrowiecki commented 2 months ago

Is your feature request related to a problem? Please describe. I would like to have a per field authorization on inputs, so users with the specific role can update some fields, and other users with different role can't.

Describe the solution you'd like

The ideal solution would be to have a possibility to mark certain fields in Input Type to require the authorization.

@InputType()
class UpdateUserInput {
    @Field()
    @Authorized(['ADMIN', 'CUSTOMER'])
    fullName: string;

    @Field()
    @Authorized('ADMIN')
    email: string;
}

If the user has a role 'ADMIN' and he tries to fire update mutation using UpdateUserInput, specifing email field then the authorization should pass an access to this update mutation.

If the user has a role 'CUSTOMER' and he tries to fire update mutation using UpdateUserInput, specifing email field then the authorization should block the user from performing this operation.

Describe alternatives you've considered I considered creating different mutation resolvers for different roles. The disadvantages of this approach are:


Please let me know what you think.

carlocorradini commented 2 months ago

Have you seen graphql-auth-directive (NOTE: I'm the author)? It's exactly what you want (see type-graphql integration) 🥳 It's missing the integration with input types but we can work together to implement it 🤞👍