A directive can be attached to a field or fragment inclusion, and can affect execution of the query in any way the server desires (read more here). The GraphQL specification provides several default directives:
@include(if: Boolean)- only include this field in the result if the argument is true
@skip(if: Boolean)- skip this field if the argument is true
@deprecated(reason: String)- marks field as deprecated with message
A directive is an identifier preceded by a
@ character, optionally followed by a list of named arguments, which can appear after almost any element in the GraphQL query and schema languages.
To create a custom schema directive, declare a class which extends the
SchemaDirectiveVisitor class exported from the
info Hint Note that directives cannot be decorated with the
@Injectable()decorator. Thus, they are not able to inject dependencies.
Now, register the
UpperCaseDirective in the
Once registered, the
@upper directive can be used in our schema. However, the way you apply the directive will vary depending on the approach you use (code first or schema first).
In the code first approach, use the
@Directive() decorator to apply the directive.
info Hint The
@Directive()decorator is exported from the
Directives can be applied on fields, field resolvers, input and object types, as well as queries, mutations, and subscriptions. Here's an example of the directive applied on the query handler level:
Directives applied through the
@Directive() decorator will not be reflected in the generated schema definition file.
In the schema first approach, apply directives directly in SDL.