Directives
GraphQL directives add metadata to schema elements. gurafi supports custom directive
definitions and applications on types, fields, field arguments and input fields; the built-in
@deprecated, @specifiedBy and @oneOf directives are produced by their dedicated builder
options.
Register a definition
A directive must be defined before it is applied. gql::DirectiveDefinition supplies the name,
description, locations, repeatability and arguments:
The definition is validated with the rest of the schema. A directive applied in a location it does not declare is a build error, as is a wrong argument type or a non-repeatable duplicate.
Apply to a type
type_directive works for objects, interfaces, unions, scalars, enums, input objects and root
types. The type may be registered later; applications are recorded by name and attached during
build. A name that is never registered is a build error.
At the declaration site, use the builder method:
Interfaces and unions have directive(...) too.
Apply to a field
FieldConfig covers object, root and subscription fields. InterfaceFieldConfig covers
interface fields; object fields that inherit an interface field also inherit its directives
unless the object declares its own.
Apply to arguments and input fields
argument_directive targets object, interface and root field arguments. It does not cover
arguments of a directive definition, which are configured directly on the InputValue.
Repeatable directives
A repeatable directive may be applied more than once:
Applications are kept in declaration order.
Where directives appear
schema.sdl()prints the definition and every application.Schema::extend_sdlandSchema::mergepreserve definitions and applications.- Standard GraphQL introspection exposes directive definitions (
__schema.directives: name, description, locations, arguments,isRepeatable). The specification has no introspection field for applied directives, so use SDL orgql::Field::directivesin a plugin to inspect applications.
Directives are metadata; they do not install resolvers or change execution semantics. If a
directive should enforce a rule, read it in a plugin or in the
resolver. Built-in directives such as @deprecated affect introspection and SDL only.
Built-in directives
Use the dedicated builder options rather than applying the directive manually:
Those fields also print as @deprecated, @specifiedBy and @oneOf and are validated as
built-in applications.
Derive attributes
Applying a custom directive through #[graphql(directive = "...")] is not supported. Builder
methods are explicit about reopening and repeatable applications. If a directive is attached in
one module, apply it from that module's registration function.
