You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can also specify custom type function for any field with `@GraphQLType` annotation.
402
402
403
403
## Directives
404
-
You can wire your fields using directives with annotations.
405
-
We allow both defining directives using annotations, and wiring fields.
404
+
In GraphQL, you can add directives to your schema. Directive is a way of adding some logic to your schema or changing your schema.
405
+
For example, we can create a `@upper` directive, that if we add it to string fields in our schema, they will be transformed to upper cases (its an example, you need to implement it).
406
406
407
-
### Creating/Defining a ``GraphQLDirective``
408
-
In order to create a directive, you first have to create a class that the directive will be created from.
409
-
For example:
407
+
### Declaring a ``GraphQLDirective``
408
+
There are multiple ways to declare a directive in your schema using graphql-java-annotations.
409
+
410
+
#### Using a Java Annotation (recommended)
411
+
This is the most recommended way of creating a directive, because it is very easy to use later in your schema.
412
+
In order to declare a directive using a java annotation, you first have to create the java annotation, and annotate it with special annotations.
413
+
414
+
For example, we wish to create a directive that adds suffix to graphql fields.
415
+
416
+
```java
417
+
@GraphQLName("suffix")
418
+
@GraphQLDescription("this directive adds suffix to a string type")
@GraphQLDescription("the suffix to add to your type")
425
+
booleansuffixToAdd() default true;
426
+
}
427
+
```
428
+
429
+
- must be annotated with `@GraphQLDirectiveDefinition` and to supply a wiring class to it (will be explained later)
430
+
- the name of the directive will be taken from the class name `(Suffix)` or if annotated with `@GraphQLName` - from its value
431
+
- the description is taken from the `@GraphQLDescription` annotation
432
+
- must be annotated with `@Retention` with a `RUNTIME` policy
433
+
- must be annotated with `@DirectiveLocations` in order to specify where we can put this directive on (for example - field definition, interface)
434
+
435
+
You can see that we also defined a ``sufixToAdd`` argument for the directive. We can also use `@GraphQLName` and `@GraphQLDescription` annotations in there.
436
+
437
+
In order to define a default value for the argument, use the `default` keyword like in the example.
438
+
439
+
After you created the class, you will be able to create the ``GraphQLDirective`` object using the following code:
@@ -450,17 +525,50 @@ public class UpperWiring implements AnnotationsDirectiveWiring {
450
525
}
451
526
```
452
527
528
+
In this example we wrap the data fetcher of the field in order to make the resolved value upper case.
529
+
453
530
You can also use the `field.transform` method in order to change some of the field's properties.
454
531
455
532
This class turns your string field to upper case if the directive argument "isActive" is set to true.
456
-
Now, you have to wire the field itself:
533
+
534
+
Put this class inside the `@GraphQLDirectiveDefinition(wiring = UpperWiring.class)` annotation where you declare your directive (see directive declaration section above).
535
+
536
+
### Using the directives
537
+
538
+
There are 2 ways of using the directives in your graphql types.
539
+
540
+
#### Using the directive java annotation (RECOMMENDED)
541
+
This way only works if you declared your directive as a java annotation.
542
+
In the example above, we created the `@Suffix` annotation as a directive.
Now every time the field will be executed, the suffix " is cool" will be added to it.
556
+
You can also use directive on field arguments, interfaces, etc.
557
+
558
+
#### Using `@GraphQLDirectives` annotation
559
+
This way works in the 3 methods of declaring directives, but is less recommended because its more complicated and not so nice.
560
+
You can annotate your graphql field with the `@GraphQLDirectives` annotation and supply it with the directives to use and the arguments values you want to supply.
We now wired the field "name" - so it will turn upper case when calling the field.
465
573
The ``Directive`` annotations requires the name of the directive, the wiring class (the ``UpperWiring`` class defined earlier), and the values of the arguments. If an argument has a default value, you don't have to supply a value in the arguments values.
0 commit comments