vendure-ecommerce / vendure

The commerce platform with customization in its DNA.
https://www.vendure.io
Other
5.8k stars 1.03k forks source link

Allow GraphQL directive to be added #1151

Open alexisvigoureux opened 3 years ago

alexisvigoureux commented 3 years ago

Is your feature request related to a problem? Please describe. I would like to create GraphQL directives to add reusable validation rules to the resolvers of some plugins.

Describe the solution you'd like Add a new optional input field when creating a plugin

@VendurePlugin({
  imports: [PluginCommonModule],
  shopApiExtensions: {
    schema: gql` ... `,
    resolvers: [/* ... */, MyCustomMutationResultResolver],
    schemaDirectives: {
       upper: UpperCaseDirective,
   }
  }
})
export class MyPlugin {}

Additional context The current alternative is to write a lot of code to check each field and throw an error.

michaelbromley commented 3 years ago

I spent a good few hours working on this today. The actual implementation is quite straightforward, but the problem was that it was not working for some unknown reason.

I spent a long time painstakingly stepping through the execution line by line, deep inside the node_modules dir, but could not figure it out.

If anyone wants to have a go on this, see the checkout the schema-directives branch - all the work is in this commit (https://github.com/vendure-ecommerce/vendure/commit/f810bdddc77ab98170726e3213d27e4f33a83ed3).

In there you can see that I created a test plugin which is based on the custom directive example from the Nest docs. It should return:

{
   hello: "HELLO"
}

but the @upper directive is having no effect.

Possibly related issues: