Skip to content

Commit cfdba31

Browse files
authored
Merge pull request #212 from Enigmatis/development
V 7.0
2 parents 8268aec + af891db commit cfdba31

File tree

57 files changed

+1233
-688
lines changed

Some content is hidden

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

57 files changed

+1233
-688
lines changed

README.md

+97-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1+
![logo](polaris-iconsmalredl.png?raw=true)
2+
# GraphQL-Java Annotations
13
[![Build Status](https://travis-ci.org/graphql-java/graphql-java-annotations.svg?branch=master)](https://travis-ci.org/graphql-java/graphql-java-annotations)
24
[![Maven Central](https://img.shields.io/maven-central/v/io.github.graphql-java/graphql-java-annotations.svg?maxAge=3000)]()
3-
# GraphQL Annotations for Java
45

56
[GraphQL-Java](https://github.com/andimarek/graphql-java) is a great library, but its syntax is a little bit verbose. This library offers an annotations-based
67
syntax for GraphQL schema definition.
78

9+
10+
## Table Of Contents
11+
- [Getting Started](#getting-started)
12+
- [GraphQLAnnotations class](#graphqlannotations-class)
13+
- [Annotations Schema Creator](#annotations-schema-creator)
14+
- [Defining Objects](#defining-objects)
15+
- [Defining Interfaces](#defining-interfaces)
16+
- [Defining Unions](#defining-unions)
17+
- [Fields](#fields)
18+
- [Custom DataFetcher](#custom-data-fetcher)
19+
- [Type Extensions](#type-extensions)
20+
- [Defining Extensions in Annotation](#defining-extensions-in-annotations)
21+
- [Data Fetching with Extensions](#data-fetching-with-extensions)
22+
- [Type Inference](#type-inference)
23+
- [Directives](#directives)
24+
- [Creating/Defining a GraphQL Directive](#creatingdefining-a-graphqldirective)
25+
- [Wiring with Directives](#wiring-with-directives)
26+
- [Relay Support](#relay-support)
27+
- [Mutations](#mutations)
28+
- [Connection](#connection)
29+
- [Customizing Relay Schema](#customizing-relay-schema)
30+
831
## Getting Started
932

1033

1134
(Gradle syntax)
1235

1336
```groovy
1437
dependencies {
15-
compile "io.github.graphql-java:graphql-java-annotations:6.1"
38+
compile "io.github.graphql-java:graphql-java-annotations:7.0"
1639
}
1740
```
1841

@@ -22,10 +45,65 @@ dependencies {
2245
<dependency>
2346
<groupId>io.github.graphql-java</groupId>
2447
<artifactId>graphql-java-annotations</artifactId>
25-
<version>6.1</version>
48+
<version>7.0</version>
2649
</dependency>
2750
```
2851

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.
29107

30108
## Defining Objects
31109

@@ -39,7 +117,8 @@ public class SomeObject {
39117
}
40118

41119
// ...
42-
GraphQLObjectType object = GraphQLAnnotations.object(SomeObject.class);
120+
GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
121+
GraphQLObjectType object = graphQLAnnotations.object(SomeObject.class);
43122
```
44123

45124
## Defining Interfaces
@@ -58,7 +137,8 @@ public class MyTypeResolver implements TypeResolver {
58137
}
59138

60139
// ...
61-
GraphQLInterfaceType object = GraphQLAnnotations.iface(SomeInterface.class);
140+
GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
141+
GraphQLInterfaceType object = graphQLAnnotations.generateInterface(SomeInterface.class);
62142
```
63143

64144
An instance of the type resolver will be created from the specified class. If a `getInstance` method is present on the
@@ -262,13 +342,11 @@ public class HumanExtension {
262342
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.
263343
All GraphQL annotations can be used on extension classes.
264344

265-
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()` :
266346

267347
```
268-
GraphQLAnnotationsProcessor processor = GraphQLAnnotations.getInstance();
269-
270348
// Register extensions
271-
processor.registerTypeExtension(HumanExtension.class);
349+
graphqlAnnotations.registerTypeExtension(HumanExtension.class);
272350
273351
// Create type
274352
GraphQLObjectType type = processor.getObject(Human.class);
@@ -314,7 +392,7 @@ public class UUIDTypeFunction implements TypeFunction {
314392
And register it with `GraphQLAnnotations`:
315393

316394
```java
317-
GraphQLAnnotations.register(new UUIDTypeFunction())
395+
graphqlAnnotations.registerType(new UUIDTypeFunction())
318396

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

349427
After you created the class, you will be able to create the ``GraphQLDirective`` object using the following code:
350428
```java
351-
GraphQLAnnotations.directive(UpperDirective.class);
429+
graphqlAnnotations.directive(UpperDirective.class);
352430
```
353431

354432
### Wiring with directives
@@ -361,16 +439,19 @@ public class UpperWiring implements AnnotationsDirectiveWiring {
361439
public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) {
362440
GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement();
363441
boolean isActive = (boolean) environment.getDirective().getArgument("isActive").getValue();
364-
DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), (((dataFetchingEnvironment, value) -> {
442+
CodeRegistryUtil.wrapDataFetcher(field, environment, (((dataFetchingEnvironment, value) -> {
365443
if (value instanceof String && isActive) {
366444
return ((String) value).toUpperCase();
367445
}
368-
return value;
369-
})));
370-
return field.transform(builder -> builder.dataFetcher(dataFetcher));
446+
return value;
447+
})));
448+
return field;
371449
}
372450
}
373451
```
452+
453+
You can also use the `field.transform` method in order to change some of the field's properties.
454+
374455
This class turns your string field to upper case if the directive argument "isActive" is set to true.
375456
Now, you have to wire the field itself:
376457
```java
@@ -410,7 +491,7 @@ NOTE: because `PropertyDataFetcher` and `FieldDataFetcher` can't handle connecti
410491
### Customizing Relay schema
411492

412493
By default, GraphQLAnnotations will use the `graphql.relay.Relay` class to create the Relay specific schema types (Mutations, Connections, Edges, PageInfo, ...).
413-
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
414495
can redefine methods that create Relay types.
415496

416497
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

+5-8
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'
@@ -169,8 +163,11 @@ bintray {
169163
version {
170164
name = project.version
171165
released = new Date()
166+
gpg {
167+
sign = false
168+
}
172169
mavenCentralSync {
173-
sync = true //[Default: true] Determines whether to sync the version to Maven Central.
170+
sync = true //[Default: true] Determines whether to sync the version to Maven Central..
174171
user = System.getenv('OSS_USER') ?: project.findProperty('OSS_USER') ?: ''
175172
password = System.getenv('OSS_PASS') ?: project.findProperty('OSS_PASS') ?: ''
176173
close = '1'

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ org.gradle.daemon=true
55
org.gradle.parallel=true
66
org.gradle.jvmargs=-Dfile.encoding=UTF-8
77

8-
version = 6.2
8+
version = 7.0

polaris-iconsmalredl.png

10.6 KB
Loading

0 commit comments

Comments
 (0)