Skip to content

Commit 36b1c03

Browse files
committed
This fixes up the example code so it used CodeRegistry and also quick rename of parameter names there
1 parent 52a7b6e commit 36b1c03

3 files changed

Lines changed: 60 additions & 29 deletions

File tree

src/main/java/graphql/schema/GraphQLCodeRegistry.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,28 +315,28 @@ public Builder dataFetchers(String parentTypeName, Map<String, DataFetcher> fiel
315315
return this;
316316
}
317317

318-
public Builder typeResolver(GraphQLInterfaceType parentType, TypeResolver typeResolver) {
319-
typeResolverMap.put(parentType.getName(), typeResolver);
318+
public Builder typeResolver(GraphQLInterfaceType interfaceType, TypeResolver typeResolver) {
319+
typeResolverMap.put(interfaceType.getName(), typeResolver);
320320
return this;
321321
}
322322

323-
public Builder typeResolverIfAbsent(GraphQLInterfaceType parentType, TypeResolver typeResolver) {
324-
typeResolverMap.putIfAbsent(parentType.getName(), typeResolver);
323+
public Builder typeResolverIfAbsent(GraphQLInterfaceType interfaceType, TypeResolver typeResolver) {
324+
typeResolverMap.putIfAbsent(interfaceType.getName(), typeResolver);
325325
return this;
326326
}
327327

328-
public Builder typeResolver(GraphQLUnionType parentType, TypeResolver typeResolver) {
329-
typeResolverMap.put(parentType.getName(), typeResolver);
328+
public Builder typeResolver(GraphQLUnionType unionType, TypeResolver typeResolver) {
329+
typeResolverMap.put(unionType.getName(), typeResolver);
330330
return this;
331331
}
332332

333-
public Builder typeResolverIfAbsent(GraphQLUnionType parentType, TypeResolver typeResolver) {
334-
typeResolverMap.putIfAbsent(parentType.getName(), typeResolver);
333+
public Builder typeResolverIfAbsent(GraphQLUnionType unionType, TypeResolver typeResolver) {
334+
typeResolverMap.putIfAbsent(unionType.getName(), typeResolver);
335335
return this;
336336
}
337337

338-
public Builder typeResolver(String parentTypeName, TypeResolver typeResolver) {
339-
typeResolverMap.put(assertValidName(parentTypeName), typeResolver);
338+
public Builder typeResolver(String typeName, TypeResolver typeResolver) {
339+
typeResolverMap.put(assertValidName(typeName), typeResolver);
340340
return this;
341341
}
342342

src/test/groovy/readme/MappingExamples.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import graphql.Scalars;
44
import graphql.schema.DataFetcher;
55
import graphql.schema.DataFetchingEnvironment;
6+
import graphql.schema.FieldCoordinates;
7+
import graphql.schema.GraphQLCodeRegistry;
68
import graphql.schema.GraphQLFieldDefinition;
79
import graphql.schema.PropertyDataFetcher;
810

@@ -11,6 +13,8 @@
1113
import java.util.List;
1214
import java.util.Map;
1315

16+
import static graphql.schema.FieldCoordinates.coordinates;
17+
1418
@SuppressWarnings({"Convert2Lambda", "unused", "ClassCanBeStatic"})
1519
public class MappingExamples {
1620

@@ -132,7 +136,12 @@ private void directWiring() {
132136
GraphQLFieldDefinition descriptionField = GraphQLFieldDefinition.newFieldDefinition()
133137
.name("description")
134138
.type(Scalars.GraphQLString)
135-
.dataFetcher(PropertyDataFetcher.fetching("desc"))
139+
.build();
140+
141+
GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry()
142+
.dataFetcher(
143+
coordinates("ObjectType", "description"),
144+
PropertyDataFetcher.fetching("desc"))
136145
.build();
137146

138147
}

src/test/groovy/readme/ReadmeExamples.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import graphql.schema.Coercing;
1414
import graphql.schema.DataFetcher;
1515
import graphql.schema.DataFetchingEnvironment;
16+
import graphql.schema.GraphQLCodeRegistry;
1617
import graphql.schema.GraphQLEnumType;
1718
import graphql.schema.GraphQLInputObjectType;
1819
import graphql.schema.GraphQLInterfaceType;
@@ -49,7 +50,9 @@
4950
import static graphql.Scalars.GraphQLBoolean;
5051
import static graphql.Scalars.GraphQLString;
5152
import static graphql.StarWarsSchema.queryType;
53+
import static graphql.schema.FieldCoordinates.coordinates;
5254
import static graphql.schema.GraphQLArgument.newArgument;
55+
import static graphql.schema.GraphQLCodeRegistry.newCodeRegistry;
5356
import static graphql.schema.GraphQLEnumType.newEnum;
5457
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
5558
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
@@ -143,23 +146,28 @@ void enumTypes() {
143146
}
144147

145148
void unionType() {
149+
TypeResolver typeResolver = new TypeResolver() {
150+
@Override
151+
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
152+
if (env.getObject() instanceof Cat) {
153+
return CatType;
154+
}
155+
if (env.getObject() instanceof Dog) {
156+
return DogType;
157+
}
158+
return null;
159+
}
160+
};
146161
GraphQLUnionType PetType = newUnionType()
147162
.name("Pet")
148163
.possibleType(CatType)
149164
.possibleType(DogType)
150-
.typeResolver(new TypeResolver() {
151-
@Override
152-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
153-
if (env.getObject() instanceof Cat) {
154-
return CatType;
155-
}
156-
if (env.getObject() instanceof Dog) {
157-
return DogType;
158-
}
159-
return null;
160-
}
161-
})
162165
.build();
166+
167+
GraphQLCodeRegistry codeRegistry = newCodeRegistry()
168+
.typeResolver("Pet", typeResolver)
169+
.build();
170+
163171
}
164172

165173

@@ -207,7 +215,13 @@ public Foo get(DataFetchingEnvironment environment) {
207215
.field(newFieldDefinition()
208216
.name("foo")
209217
.type(GraphQLString)
210-
.dataFetcher(fooDataFetcher))
218+
)
219+
.build();
220+
221+
GraphQLCodeRegistry codeRegistry = newCodeRegistry()
222+
.dataFetcher(
223+
coordinates("ObjectType", "foo"),
224+
fooDataFetcher)
211225
.build();
212226

213227
}
@@ -242,14 +256,14 @@ private ReviewStore reviewStore() {
242256

243257
void mutationExample() {
244258

245-
GraphQLInputObjectType episodeType = GraphQLInputObjectType.newInputObject()
259+
GraphQLInputObjectType episodeType = newInputObject()
246260
.name("Episode")
247261
.field(newInputObjectField()
248262
.name("episodeNumber")
249263
.type(Scalars.GraphQLInt))
250264
.build();
251265

252-
GraphQLInputObjectType reviewInputType = GraphQLInputObjectType.newInputObject()
266+
GraphQLInputObjectType reviewInputType = newInputObject()
253267
.name("ReviewInput")
254268
.field(newInputObjectField()
255269
.name("stars")
@@ -281,13 +295,21 @@ void mutationExample() {
281295
.name("review")
282296
.type(reviewInputType)
283297
)
284-
.dataFetcher(mutationDataFetcher())
285298
)
286299
.build();
287300

301+
GraphQLCodeRegistry codeRegistry = newCodeRegistry()
302+
.dataFetcher(
303+
coordinates("CreateReviewForEpisodeMutation", "createReview"),
304+
mutationDataFetcher()
305+
)
306+
.build();
307+
308+
288309
GraphQLSchema schema = GraphQLSchema.newSchema()
289310
.query(queryType)
290311
.mutation(createReviewForEpisodeMutation)
312+
.codeRegistry(codeRegistry)
291313
.build();
292314
}
293315

@@ -467,7 +489,7 @@ private Directive getDirective(FieldDefinition fieldDefintion, String type) {
467489
}
468490

469491

470-
public static GraphQLScalarType CustomScalar = new GraphQLScalarType("Custom", "Custom Scalar", new Coercing<Integer, Integer>() {
492+
public static GraphQLScalarType CustomScalar = GraphQLScalarType.newScalar().name("Custom").description("Custom Scalar").coercing(new Coercing<Integer, Integer>() {
471493
@Override
472494
public Integer serialize(Object input) {
473495
throw new UnsupportedOperationException("Not implemented");
@@ -482,7 +504,7 @@ public Integer parseValue(Object input) {
482504
public Integer parseLiteral(Object input) {
483505
throw new UnsupportedOperationException("Not implemented");
484506
}
485-
});
507+
}).build();
486508

487509
private File loadSchema(String s) {
488510
return null;

0 commit comments

Comments
 (0)