How can one get the values passed to a directive resolver applied to a mutation in Prisma?



I’ve checked out the post regarding directive permissions:

I think it looks really attractive so I’ve started trying to implement some of the stuff.

But I’m stuck on how to get the values passed in as arguments to the mutation in my custom directive resolvers?

In the example in the blog post linked to above there is the following example:

type Mutation {
    updateAskingPrice(id: ID!, newPrice: Float!): Vehicle! @hasRole(role: "MANAGER")

And that’s fine as long as I don’t care about the data passed in to the updateAskingPrice mutation.

But what if I would like to allow an ‘ACCOUNTANT’ role to update the price when it is > 1000. But if the price is <= 1000 then only the ‘MANAGER’ should be able to update it?

Then I would need access to the mutation argument values in the directive resolver from the example:

  hasRole: (next, source, {role}, ctx) => {
    // How do I get the newPrice value here?
    const user = getUser()
    if (role === user.role) return next();
    throw new Error(`Must have role: ${role}, you have role: ${user.role}`)

I’ve tried to debug this and can see that the value can be accessed unparsed in ctx.request.body.query propery.

Maybe there is a way to parse it with some built in function?

Then I would have a solution.

But ideally I would like to place a directive on the input argument newPrice directly. But that doesn’t seem to be possible?

When I try the server doesn’t complain about the schema when I do this, but my custom directive resolver is never triggered then. If I could do that I could have a much simpler syntax for the parameter to my directive. Could be something like @limitRole({ limit: 1000, belowOrEqualRole: 'MANAGER', aboveRole: 'ACCOUNTANT'}).

This would also allow me to write validations as directives.