Skip to content

Commit 51774f0

Browse files
authored
Merge pull request #209 from yarinvak/graphql-java-12
Graphql java 12
2 parents a9c49dd + 84c738d commit 51774f0

File tree

55 files changed

+1133
-689
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1133
-689
lines changed

README.md

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
![logo](polaris-iconsmalredl.png?raw=true)
2-
# GraphQL Annotations for Java
2+
# GraphQL-Java Annotations
33
[![Build Status](https://travis-ci.org/graphql-java/graphql-java-annotations.svg?branch=master)](https://travis-ci.org/graphql-java/graphql-java-annotations)
44
[![Maven Central](https://img.shields.io/maven-central/v/io.github.graphql-java/graphql-java-annotations.svg?maxAge=3000)]()
55

@@ -9,6 +9,8 @@ syntax for GraphQL schema definition.
99

1010
## Table Of Contents
1111
- [Getting Started](#getting-started)
12+
- [GraphQLAnnotations class](#graphqlannotations-class)
13+
- [Annotations Schema Creator](#annotations-schema-creator)
1214
- [Defining Objects](#defining-objects)
1315
- [Defining Interfaces](#defining-interfaces)
1416
- [Defining Unions](#defining-unions)
@@ -47,6 +49,61 @@ dependencies {
4749
</dependency>
4850
```
4951

52+
The graphql-java-annotations library is able to create GraphQLType objects out of your Java classes.
53+
These GraphQLType objects can be later injected into the graphql-java schema.
54+
55+
graphql-java-annotations also allows you to wire your objects with data fetchers and type resolvers while annotating your fields/types. The result of this process will be a ``GraphQLCodeRegistry.Builder`` object that can be later built and injected to the graphql-java schema.
56+
57+
58+
## GraphQLAnnotations class
59+
60+
You can create an instance of the `GraphQLAnnotations` class in order to create the GraphQL types.
61+
```java
62+
GraphQLAnnotations graphqlAnnotations = new GraphQLAnnotations();
63+
```
64+
65+
Using this object, you will be able to create the GraphQL types.
66+
There are few types that can be generated - a `GraphQLObjectType`, a `GraphQLInterfaceType` and a `GraphQLDirective`.
67+
68+
```java
69+
GraphQLObjectType query = graphqlAnnotations.object(Query.class);
70+
GraphQLDirective upperDirective = graphqlAnnotations.directive(UpperDirective.class);
71+
GraphQLInterfaceType myInterface = graphqlAnnotations.generateInterface(MyInterface.class);
72+
```
73+
74+
Then you can use these types in order to create a graphql-java schema.
75+
But, in order to create a graphql-java schema, you need also the ``GraphQLCodeRegistry``, which contains all the data fetchers mapped to their fields (and also type resolvers).
76+
77+
You can obtain the code registry this way:
78+
79+
```java
80+
graphqlAnnotations.getContainer().getCodeRegistryBuilder().build();
81+
```
82+
83+
## Annotations Schema Creator
84+
85+
Using the `GraphQLAnnotations` processor object can be a little bit confusing if you wish to use it to create a GraphQL schema.
86+
So we created a util class to help you create your desired GraphQL schema, in a syntax similiar to the graphql-java syntax.
87+
88+
In order to do so you can use the ``AnnotationsSchemaCreator.Builder`` in the following way:
89+
90+
```java
91+
GraphQLSchema schema = AnnotationsSchemaCreator.newAnnotationsSchema()
92+
.query(Query.class) // to create you query object
93+
.mutation(Mutation.class) // to create your mutation object
94+
.subscription(Subscription.class) // to create your subscription object
95+
.directive(UpperDirective.class) // to create a directive
96+
.additionalType(AdditionalType.class) // to create some additional type and add it to the schema
97+
.typeFunction(CustomType.class) // to add a typefunction
98+
.setAlwaysPrettify(true) // to set the global prettifier of field names (removes get/set/is prefixes from names)
99+
.setRelay(customRelay) // to add a custom relay object
100+
.build();
101+
```
102+
103+
Of course you can use this builder with only some of the properties, but the query class must be provided.
104+
note - The GraphQLSchema is a graphql-java type.
105+
106+
Continue reading in order to understand how your java classes should look in order to be provided to the annotations schema creator.
50107

51108
## Defining Objects
52109

@@ -60,7 +117,8 @@ public class SomeObject {
60117
}
61118

62119
// ...
63-
GraphQLObjectType object = GraphQLAnnotations.object(SomeObject.class);
120+
GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
121+
GraphQLObjectType object = graphQLAnnotations.object(SomeObject.class);
64122
```
65123

66124
## Defining Interfaces
@@ -79,7 +137,8 @@ public class MyTypeResolver implements TypeResolver {
79137
}
80138

81139
// ...
82-
GraphQLInterfaceType object = GraphQLAnnotations.iface(SomeInterface.class);
140+
GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
141+
GraphQLInterfaceType object = graphQLAnnotations.generateInterface(SomeInterface.class);
83142
```
84143

85144
An instance of the type resolver will be created from the specified class. If a `getInstance` method is present on the
@@ -283,13 +342,11 @@ public class HumanExtension {
283342
Classes marked as "extensions" will actually not define a new type, but rather set new fields on the class it extends when it will be created.
284343
All GraphQL annotations can be used on extension classes.
285344

286-
Extensions are registered in GraphQLAnnotationProcessor by using `registerTypeExtension`. Note that extensions must be registered before the type itself is requested with `getObject()` :
345+
Extensions are registered in GraphQLAnnotations object by using `registerTypeExtension`. Note that extensions must be registered before the type itself is requested with `getObject()` :
287346

288347
```
289-
GraphQLAnnotationsProcessor processor = GraphQLAnnotations.getInstance();
290-
291348
// Register extensions
292-
processor.registerTypeExtension(HumanExtension.class);
349+
graphqlAnnotations.registerTypeExtension(HumanExtension.class);
293350
294351
// Create type
295352
GraphQLObjectType type = processor.getObject(Human.class);
@@ -335,7 +392,7 @@ public class UUIDTypeFunction implements TypeFunction {
335392
And register it with `GraphQLAnnotations`:
336393

337394
```java
338-
GraphQLAnnotations.register(new UUIDTypeFunction())
395+
graphqlAnnotations.registerType(new UUIDTypeFunction())
339396

340397
// or if not using a static version of GraphQLAnnotations:
341398
// new GraphQLAnnotations().registerType(new UUIDTypeFunction())
@@ -369,7 +426,7 @@ You can also use ``@GraphQLName`` and ``@GraphQLDescription`` annotations on the
369426

370427
After you created the class, you will be able to create the ``GraphQLDirective`` object using the following code:
371428
```java
372-
GraphQLAnnotations.directive(UpperDirective.class);
429+
graphqlAnnotations.directive(UpperDirective.class);
373430
```
374431

375432
### Wiring with directives
@@ -382,16 +439,19 @@ public class UpperWiring implements AnnotationsDirectiveWiring {
382439
public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) {
383440
GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement();
384441
boolean isActive = (boolean) environment.getDirective().getArgument("isActive").getValue();
385-
DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), (((dataFetchingEnvironment, value) -> {
442+
CodeRegistryUtil.wrapDataFetcher(field, environment, (((dataFetchingEnvironment, value) -> {
386443
if (value instanceof String && isActive) {
387444
return ((String) value).toUpperCase();
388445
}
389-
return value;
390-
})));
391-
return field.transform(builder -> builder.dataFetcher(dataFetcher));
446+
return value;
447+
})));
448+
return field;
392449
}
393450
}
394451
```
452+
453+
You can also use the `field.transform` method in order to change some of the field's properties.
454+
395455
This class turns your string field to upper case if the directive argument "isActive" is set to true.
396456
Now, you have to wire the field itself:
397457
```java
@@ -431,7 +491,7 @@ NOTE: because `PropertyDataFetcher` and `FieldDataFetcher` can't handle connecti
431491
### Customizing Relay schema
432492

433493
By default, GraphQLAnnotations will use the `graphql.relay.Relay` class to create the Relay specific schema types (Mutations, Connections, Edges, PageInfo, ...).
434-
It is possible to set a custom implementation of the Relay class with `GraphQLAnnotations.setRelay` method. The class should inherit from `graphql.relay.Relay` and
494+
It is possible to set a custom implementation of the Relay class with `graphqlAnnotations.setRelay` method. The class should inherit from `graphql.relay.Relay` and
435495
can redefine methods that create Relay types.
436496

437497
It is also possible to specify for every connection which relay do you want to use, by giving a value to the annotation:

build.gradle

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
3737
from javadoc.destinationDir
3838
}
3939

40-
idea {
41-
project {
42-
languageLevel = '1.8'
43-
vcs = 'Git'
44-
}
45-
}
4640
release {
4741
tagTemplate = 'v${version}'
4842
failOnPublishNeeded = false
@@ -69,7 +63,7 @@ gradle.projectsEvaluated {
6963

7064
dependencies {
7165
compile 'javax.validation:validation-api:1.1.0.Final'
72-
compile 'com.graphql-java:graphql-java:11.0'
66+
compile 'com.graphql-java:graphql-java:12.0'
7367

7468
// OSGi
7569
compileOnly 'org.osgi:org.osgi.core:6.0.0'

0 commit comments

Comments
 (0)