Using composable directives

πŸ“£ Note: This functionality will be available soon.

Have a directive modify the behavior of another directive.

This functionality allows to be able to use a directive, when otherwise we could not use it due to a type mismatch (i.e. when a directive cannot be applied on the field, because it has an input which is different than the field's output).

For instance, field capabilities returns [String] (an array of strings), and directive @upperCase receives String. Hence, executing the following query returns an error due to the type mismatch:

query {
user(id: 1) {
capabilities @upperCase
}
}

With composable directives, we can employ directive @forEach (which iterates over an array of elements, and applies its nested directive on each of them) to set the stage before @upperCase is executed, making it receive a single element (of type String).

The query from above can be satisfied like this:

query {
user(id: 1) {
capabilities
@forEach
@upperCase(nestedUnder: -1)
}
}

Meta-directives permalink

This module introduces two "meta-directives":

  1. @forEach iterates over a list of elements from the queried entity, and passes a reference to the iterated element to the next directive.
  2. @advancePointersInArray makes the next directive receive an element from under some path within the queried array/object.

Directives are nested under any of these two meta-directives via argument nestedUnder, which receives a negative integer defining the composing directive's relative position.

In the example below, we have:

  • @forEach is the composing directive
  • @translate is added under @forEach
{
someField
@forEach
@translate(nestedUnder:-1)
}

Meta-directives can also be nested within meta-directives.

In the example below, we have:

  • @forEach is the topmost composing directive
  • @translate and @advancePointersInArray are added under @forEach
  • @upperCase is added under @advancePointersInArray:
{
someField
@forEach
@translate(nestedUnder:-1)
@advancePointersInArray(nestedUnder:-2, path: 0) # Only 1st element in list
@upperCase(nestedUnder:-1)
}

GraphQL spec permalink

This functionality is currently not part of the GraphQL spec, but a related one has been requested: