status-im/nim-graphql

Add support for custom directives

Opened this issue · 1 comments

we have at least two types of directives:

  1. schema directives
  2. query directives

but we also have lots of unanswered questions:

  1. where in the execution/validation pipeline we should put custom directives execution?
  2. how do we expose the internal of nim-graphql to custom directives?
  3. how custom directives can safely manipulate things/AST?

after #80, now we have instrumentation infrastructure in place.

  • can we extend this new feature to implement custom directives?

We will not execute directives like we are executing resolvers. we can't treat a directive like a function. Although it has parameters like a function, those parameters actually configuration parameter to adjust directive behavior. They are not parameters like a function. What I mean is, we cannot attach a function to directive and then execute that function by validator/executor. A scalar or a field resolver can work in that way because they have specific role in graphql system. But a directive can have a very broad unspecified role.

For example the @Skip and @include directives. When the validator encounter those directives, it become a flag that a field should be included or not in final execution. Although those two directives are attached to query field, but they will modify field set, not only modify a single field. We cannot limit what kind of environment(e.g. only a query field) we should pass to a directive handler. That's why a directive need more privileges compared to scalars or resolvers.

execution approach:

  • Like an instrument, a directive can scan the AST for the presence of a @directive in use.
  • Or the validator/executor when encounter a @directive, will call that 'directive instrument'.
  • Or the directive implementer can specify how the directive should be executed like when an instrument will be executed.
    This still have to be explored again, because it's still unclear how we should execute custom directives.

may be we can collect whatever custom directives out there and try to implement it using our instrument.