Skip to content

Commit 54829a2

Browse files
committed
add readme section for directives
1 parent ce00a54 commit 54829a2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,67 @@ GraphQLAnnotations.register(new UUIDTypeFunction())
322322

323323
You can also specify custom type function for any field with `@GraphQLType` annotation.
324324

325+
## Directives
326+
You can wire your fields using directives with annotations.
327+
We allow both defining directives using annotations, and wiring fields.
328+
329+
### Creating/Defining a ``GraphQLDirective``
330+
In order to create a directive, you first have to create a class that the directive will be created from.
331+
For example:
332+
333+
```java
334+
@GraphQLName("upper")
335+
@GraphQLDescription("upper")
336+
@DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.INTERFACE})
337+
public static class UpperDirective {
338+
private boolean isActive = true;
339+
}
340+
```
341+
342+
The name of the directive will be taken from the ``@GraphQLName`` annotation (if not specified, the name will be the class's name).
343+
The description of the directive will be taken from the ``@GraphQLDescription`` annotation's value.
344+
The valid locations of the directive (locations which the directive can be applied on) will be taken from ``@DirectiveLocations``.
345+
The arguments of the directive will be taken from the fields defined in the class - notice that you can only use primitive types as arguments of a directive.
346+
For example, we defined an ``isActive`` field - which is boolean, and it's default value is true. That's how the argument of the directive will be defined.
347+
You can also use ``@GraphQLName`` and ``@GraphQLDescription`` annotations on the field.
348+
349+
After you created the class, you will be able to create the ``GraphQLDirective`` object with the following code:
350+
```java
351+
GraphQLAnnotations.directive(UpperDirective.class);
352+
```
353+
354+
### Wiring with directives
355+
Using directives you will be able to wire fields and more, for example, changing the data fetchers of the fields.
356+
357+
In order to define a wiring functionality, you have to create a Wiring class matching one of you directives. For example:
358+
```java
359+
public class UpperWiring implements AnnotationsDirectiveWiring {
360+
@Override
361+
public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) {
362+
GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement();
363+
boolean isActive = (boolean) environment.getDirective().getArgument("isActive").getValue();
364+
DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), (((dataFetchingEnvironment, value) -> {
365+
if (value instanceof String && isActive) {
366+
return ((String) value).toUpperCase();
367+
}
368+
return value;
369+
})));
370+
return field.transform(builder -> builder.dataFetcher(dataFetcher));
371+
}
372+
}
373+
```
374+
This class turns you string field to upper case if the directive argument "isActive" is set to true.
375+
Now, you have to wire the field itself:
376+
```java
377+
@GraphQLField
378+
@GraphQLDirectives(@Directive(name = "upperCase", wiringClass = UpperWiring.class, argumentsValues = {"true"}))
379+
public static String name() {
380+
return "yarin";
381+
}
382+
```
383+
We now wired the field "name" - so it will turn upper case when calling the field.
384+
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.
385+
325386
## Relay support
326387

327388
### Mutations

0 commit comments

Comments
 (0)