Docblock tags
Directive annotations
On this page
To annotate the constructs of your schema with GraphQL directives, use the @gqlAnnotate docblock tag.
/** @gqlDirective on FIELD_DEFINITION */
function myDirective() {
// ...
}
/**
* @gqlQueryField
* @gqlAnnotate myDirective
*/
export function greet(): string {
return "Hello";
}directive @myDirective on FIELD_DEFINITION
type Query {
greet: String @myDirective
}A directive annotation can also pass arguments, in GraphQL's syntax for arguments:
/** @gqlDirective on FIELD_DEFINITION */
function myDirective(args: { someArg: string }) {
// ...
}
/**
* @gqlQueryField
* @gqlAnnotate myDirective(someArg: "Some String")
*/
export function greet(): string {
return "Hello";
}directive @myDirective(someArg: String!) on FIELD_DEFINITION
type Query {
greet: String @myDirective(someArg: "Some String")
}Note
The GraphQL directive @deprecated is special: it is a built-in GraphQL directive and has a built-in TypeScript equivalent, the @deprecated docblock tag. So a TypeScript @deprecated docblock tag becomes a GraphQL @deprecated directive.
Directive validation
The GraphQL specification does not require the arguments of a directive used in the schema to be validated against the directive's definition, but Gratos validates them: the arguments of each @gqlAnnotate are typechecked against its directive's definition.
Schema directive annotations at runtime
The directive annotations of your schema are in the generated .graphql file. For directives meant for clients or other infrastructure, that is enough.
A directive meant to act during execution must be in the graphql-js GraphQLSchema that Gratos generates. GraphQLSchema has no first-class place for directive annotations, so Gratos writes them in the extensions object of the relevant graphql-js object, under a grats key. The generated schema of the sample above holds:
extensions: {
grats: {
directives: [{
name: "myDirective",
args: {
someArg: "Some String"
}
}]
}
},The key stays grats in Gratos, so code written against Grats' generated schemas keeps working. The Permissions guide reads it to check an @assert directive.