Skip to content

Commit aa8b5f1

Browse files
author
Thomas Draier
committed
Added type extension documentation #112
1 parent 1628077 commit aa8b5f1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,77 @@ field.
113113

114114
You can specify a custom data fetcher for a field with `@GraphQLDataFetcher`
115115

116+
## Type extensions
117+
118+
Having one single class declaring all fields in a graphQL object type is not always possible, or can lead to huge classes.
119+
Modularizing the schema by defining fields in different classes allows you to split it in smaller chunks of codes.
120+
In IDL, this is usually written by using the `extend` keyword on top of a type definition. So you have a type defined like this :
121+
122+
```
123+
type Human {
124+
id: ID!
125+
name: String!
126+
}
127+
```
128+
129+
It would be possible to extend it later on by using the following syntax :
130+
131+
```
132+
extend type Human {
133+
homePlanet: String
134+
}
135+
```
136+
137+
### Defining extensions in annotations
138+
139+
This is possible when using annotations by registering "extensions" classes, corresponding to `extend` clauses, before creating the objects with the GraphQLAnnotationsProcessor.
140+
Extension classes are simple classes, using the same annotations, with an additional `@GraphQLTypeExtension` on the class itself. The annotation value is required and will be the class that it actually extends.
141+
142+
So the previous schema could be defined by the following classes :
143+
144+
```
145+
@GraphQLName("Human")
146+
public class Human {
147+
@GraphQLField
148+
public String name() { }
149+
}
150+
```
151+
152+
```
153+
@GraphQLTypeExtension(Human.class)
154+
public class HumanExtension {
155+
@GraphQLField
156+
public String homePlanet() { }
157+
}
158+
```
159+
160+
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.
161+
All GraphQL annotations can be used on extension classes.
162+
163+
Extensions are registered in GraqhQLAnnotationProcessor by using `registerTypeExtension`. Note that extensions must be registered before the type itself is requested with `getObject()`.
164+
165+
### Data fetching with extensions
166+
167+
As opposed to standard annotated classes mapped to GraphQL types, no instance of the extensions are created by default.
168+
In DataFetcher, the source object will still be an instance of the extended class.
169+
It is however possible to provide a constructor taking the extended class as parameter. This constructor will be used to create an instance of the extension class when a field with the default DataFetcher (without `@DataFetcher`) will be queried.
170+
If no such constructor is provided, the field must either be declared as `static` or marked as `@GraphQLInvokeDetached`. Original source object can be found in the `DataFetchingEnvironment`.
171+
172+
```
173+
@GraphQLTypeExtension(Human.class)
174+
public class HumanExtension {
175+
176+
public HumanExtension(Human human) {
177+
this.human = human;
178+
}
179+
180+
@GraphQLField
181+
public String homePlanet() {
182+
// get value somehow from human object
183+
}
184+
}
185+
```
186+
116187
## Type Inference
117188

118189
By default, standard GraphQL types (String, Integer, Long, Float, Boolean, Enum, List) will be inferred from Java types. Also, it will respect `@javax.validation.constraints.NotNull` annotation with respect to value's nullability, as well as `@GraphQLNonNull`

0 commit comments

Comments
 (0)