mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.33k stars 234 forks source link

Support custom directives in mercurius core #989

Open brainrepo opened 1 year ago

brainrepo commented 1 year ago

Mercurius doesn't offer any tool for easy handling of custom directives. The easiest way to do it is documented here: https://github.com/mercurius-js/mercurius/pull/982. This pr uses graphql-tools to simplify the schema change process. This issue is to explore 2 possible ways:

  1. create a plugin that helps to hide all the schema work.
  2. explore the possibility of integrating some custom directives APIs directly inside Mercurius

For point 1. the plugin API could be like what is done in the Mercurius auth plugin

const schema = `
  directive @redact on FIELD_DEFINITION

  type Query {
    ...
  }
`

const resolvers = {
   ...
}

app.register(mercurius, {
  schema,
  resolvers
})

app.register(mercuriusCustomDirective, {
  redact: {
    mapperKind: 'OBJECT_FIELD',
    resolver: redact(authDirectiveAST, parent, args, context, info) {
       ...
     },
  }
})

app.listen({ port: 3000 })

Integrating the custom directives feature into Mercurius che allows us to reduce the number of schema traversals. We can also take the opportunity from this work to extract some schema mapping and traversal to an external lib that can act as a partial replacement of graphql-tools #990

@simoneb