Skip to content

Commit 8dd67dd

Browse files
committed
readme fixes to comply with new PR
added setRelay method to the schema creator
1 parent f10ee5b commit 8dd67dd

File tree

2 files changed

+84
-19
lines changed

2 files changed

+84
-19
lines changed

README.md

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -33,7 +35,7 @@ syntax for GraphQL schema definition.
3335

3436
```groovy
3537
dependencies {
36-
compile "io.github.graphql-java:graphql-java-annotations:6.2"
38+
compile "io.github.graphql-java:graphql-java-annotations:XX"
3739
}
3840
```
3941

@@ -43,10 +45,65 @@ dependencies {
4345
<dependency>
4446
<groupId>io.github.graphql-java</groupId>
4547
<artifactId>graphql-java-annotations</artifactId>
46-
<version>6.2</version>
48+
<version>XX</version>
4749
</dependency>
4850
```
4951

52+
The graphql-java-annotations library is able to create GraphQLType objects out of you Java classes.
53+
These GraphQLType objects can be later injected into the graphql-java schema.
54+
55+
graphql-java-annotations also allow 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 of 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 confused 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:

src/main/java/graphql/annotations/AnnotationsSchemaCreator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -87,6 +87,11 @@ public Builder setAlwaysPrettify(Boolean shouldAlwaysPrettify) {
8787
return this;
8888
}
8989

90+
public Builder setRelay(Relay relay) {
91+
this.graphQLAnnotations.setRelay(relay);
92+
return this;
93+
}
94+
9095
public GraphQLAnnotations getGraphQLAnnotations() {
9196
return this.graphQLAnnotations;
9297
}

0 commit comments

Comments
 (0)