typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

Add support for enhancing resolvers with decorators by CRUD scope

Open MichalLytek opened this issue 2 years ago • 3 comments

I am currently working on a project, using TypeGraphQL-Prisma, where I often encounter the case of 'any authenticated user can Read objects, but only admins can Create, Update, or Delete'.

as such my enhance maps look like this:

const resolversEnhanceMap: ResolversEnhanceMap = {
  Event: {
    event: [Authorized()],
    events: [Authorized()],
    findFirstEvent: [Authorized()],
    aggregateEvent: [Authorized()],
    createEvent: [Authorized<Role>(Role.ADMIN)],
    createManyEvent: [Authorized<Role>(Role.ADMIN)],
    upsertEvent: [Authorized<Role>(Role.ADMIN)],
    updateEvent: [Authorized<Role>(Role.ADMIN)],
    updateManyEvent: [Authorized<Role>(Role.ADMIN)],
    deleteEvent: [Authorized<Role>(Role.ADMIN)],
    deleteManyEvent: [Authorized<Role>(Role.ADMIN)],
  },
};

Might it be benificial to implement, in addition to the _all shorthand, shorthands like _create, _update, _read, and _delete?
It seems like this could already greatly reduce the amount of code needed to specify separate config for queries and mutations that belong together.

something like

const resolversEnhanceMap: ResolversEnhanceMap = {
  Event: {
    _create: [Authorized<Role>(Role.ADMIN)]
    _read: [Authorized()],
    _update: [Authorized<Role>(Role.ADMIN)]
    _delete: [Authorized<Role>(Role.ADMIN)]
  },
};

or even

const resolversEnhanceMap: ResolversEnhanceMap = {
  Event: {
    _mutate: [Authorized<Role>(Role.ADMIN)]
    _query: [Authorized()],
  },
};

This could still work in conjunction with the proposed overrides, if needed.

Originally posted by @MitchJans in https://github.com/MichalLytek/typegraphql-prisma/issues/136#issuecomment-1164228825

MichalLytek avatar Jun 23 '22 11:06 MichalLytek

I have the very exact use case and would love to see this implemented.

djemeljanovs avatar Sep 05 '22 06:09 djemeljanovs

This would be amazing!

slavik0329 avatar Apr 24 '23 14:04 slavik0329

I am also interested in this feature, it will make things easier for scoped authorisations!

suiramdev avatar Jul 05 '23 10:07 suiramdev