Skip to content

Commit 5bb76aa

Browse files
committed
graphql-java#308 - males for a more fluent description of the schema
1 parent f4ebaa1 commit 5bb76aa

5 files changed

Lines changed: 101 additions & 52 deletions

File tree

‎src/main/java/graphql/introspection/Introspection.java‎

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
package graphql.introspection;
22

33

4-
import graphql.schema.*;
4+
import graphql.schema.DataFetcher;
5+
import graphql.schema.DataFetchingEnvironment;
6+
import graphql.schema.GraphQLArgument;
7+
import graphql.schema.GraphQLDirective;
8+
import graphql.schema.GraphQLEnumType;
9+
import graphql.schema.GraphQLEnumValueDefinition;
10+
import graphql.schema.GraphQLFieldDefinition;
11+
import graphql.schema.GraphQLFieldsContainer;
12+
import graphql.schema.GraphQLInputObjectField;
13+
import graphql.schema.GraphQLInputObjectType;
14+
import graphql.schema.GraphQLInterfaceType;
15+
import graphql.schema.GraphQLList;
16+
import graphql.schema.GraphQLNonNull;
17+
import graphql.schema.GraphQLObjectType;
18+
import graphql.schema.GraphQLScalarType;
19+
import graphql.schema.GraphQLSchema;
20+
import graphql.schema.GraphQLTypeReference;
21+
import graphql.schema.GraphQLUnionType;
22+
import graphql.schema.SchemaUtil;
523

624
import java.util.ArrayList;
725
import java.util.List;
@@ -10,6 +28,8 @@
1028
import static graphql.Scalars.GraphQLString;
1129
import static graphql.schema.GraphQLArgument.newArgument;
1230
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
31+
import static graphql.schema.GraphQLList.list;
32+
import static graphql.schema.GraphQLNonNull.nonNull;
1333
import static graphql.schema.GraphQLObjectType.newObject;
1434

1535
public class Introspection {
@@ -68,24 +88,24 @@ public Object get(DataFetchingEnvironment environment) {
6888
.name("__InputValue")
6989
.field(newFieldDefinition()
7090
.name("name")
71-
.type(new GraphQLNonNull(GraphQLString)))
91+
.type(nonNull(GraphQLString)))
7292
.field(newFieldDefinition()
7393
.name("description")
7494
.type(GraphQLString))
7595
.field(newFieldDefinition()
7696
.name("type")
77-
.type(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
97+
.type(nonNull(new GraphQLTypeReference("__Type"))))
7898
.field(newFieldDefinition()
7999
.name("defaultValue")
80100
.type(GraphQLString)
81101
.dataFetcher(new DataFetcher() {
82102
@Override
83103
public Object get(DataFetchingEnvironment environment) {
84104
if (environment.getSource() instanceof GraphQLArgument) {
85-
GraphQLArgument inputField = (GraphQLArgument) environment.getSource();
105+
GraphQLArgument inputField = environment.getSource();
86106
return inputField.getDefaultValue() != null ? inputField.getDefaultValue().toString() : null;
87107
} else if (environment.getSource() instanceof GraphQLInputObjectField) {
88-
GraphQLInputObjectField inputField = (GraphQLInputObjectField) environment.getSource();
108+
GraphQLInputObjectField inputField = environment.getSource();
89109
return inputField.getDefaultValue() != null ? inputField.getDefaultValue().toString() : null;
90110
}
91111
return null;
@@ -98,13 +118,13 @@ public Object get(DataFetchingEnvironment environment) {
98118
.name("__Field")
99119
.field(newFieldDefinition()
100120
.name("name")
101-
.type(new GraphQLNonNull(GraphQLString)))
121+
.type(nonNull(GraphQLString)))
102122
.field(newFieldDefinition()
103123
.name("description")
104124
.type(GraphQLString))
105125
.field(newFieldDefinition()
106126
.name("args")
107-
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__InputValue))))
127+
.type(nonNull(list(nonNull(__InputValue))))
108128
.dataFetcher(new DataFetcher() {
109129
@Override
110130
public Object get(DataFetchingEnvironment environment) {
@@ -114,10 +134,10 @@ public Object get(DataFetchingEnvironment environment) {
114134
}))
115135
.field(newFieldDefinition()
116136
.name("type")
117-
.type(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
137+
.type(nonNull(new GraphQLTypeReference("__Type"))))
118138
.field(newFieldDefinition()
119139
.name("isDeprecated")
120-
.type(new GraphQLNonNull(GraphQLBoolean))
140+
.type(nonNull(GraphQLBoolean))
121141
.dataFetcher(new DataFetcher() {
122142
@Override
123143
public Object get(DataFetchingEnvironment environment) {
@@ -135,17 +155,17 @@ public Object get(DataFetchingEnvironment environment) {
135155
.name("__EnumValue")
136156
.field(newFieldDefinition()
137157
.name("name")
138-
.type(new GraphQLNonNull(GraphQLString)))
158+
.type(nonNull(GraphQLString)))
139159
.field(newFieldDefinition()
140160
.name("description")
141161
.type(GraphQLString))
142162
.field(newFieldDefinition()
143163
.name("isDeprecated")
144-
.type(new GraphQLNonNull(GraphQLBoolean))
164+
.type(nonNull(GraphQLBoolean))
145165
.dataFetcher(new DataFetcher() {
146166
@Override
147167
public Object get(DataFetchingEnvironment environment) {
148-
GraphQLEnumValueDefinition enumValue = (GraphQLEnumValueDefinition) environment.getSource();
168+
GraphQLEnumValueDefinition enumValue = environment.getSource();
149169
return enumValue.isDeprecated();
150170
}
151171
}))
@@ -246,7 +266,7 @@ public Object get(DataFetchingEnvironment environment) {
246266
.name("__Type")
247267
.field(newFieldDefinition()
248268
.name("kind")
249-
.type(new GraphQLNonNull(__TypeKind))
269+
.type(nonNull(__TypeKind))
250270
.dataFetcher(kindDataFetcher))
251271
.field(newFieldDefinition()
252272
.name("name")
@@ -256,31 +276,31 @@ public Object get(DataFetchingEnvironment environment) {
256276
.type(GraphQLString))
257277
.field(newFieldDefinition()
258278
.name("fields")
259-
.type(new GraphQLList(new GraphQLNonNull(__Field)))
279+
.type(list(nonNull(__Field)))
260280
.argument(newArgument()
261281
.name("includeDeprecated")
262282
.type(GraphQLBoolean)
263283
.defaultValue(false))
264284
.dataFetcher(fieldsFetcher))
265285
.field(newFieldDefinition()
266286
.name("interfaces")
267-
.type(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
287+
.type(list(nonNull(new GraphQLTypeReference("__Type"))))
268288
.dataFetcher(interfacesFetcher))
269289
.field(newFieldDefinition()
270290
.name("possibleTypes")
271-
.type(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
291+
.type(list(nonNull(new GraphQLTypeReference("__Type"))))
272292
.dataFetcher(possibleTypesFetcher))
273293
.field(newFieldDefinition()
274294
.name("enumValues")
275-
.type(new GraphQLList(new GraphQLNonNull(__EnumValue)))
295+
.type(list(nonNull(__EnumValue)))
276296
.argument(newArgument()
277297
.name("includeDeprecated")
278298
.type(GraphQLBoolean)
279299
.defaultValue(false))
280300
.dataFetcher(enumValuesTypesFetcher))
281301
.field(newFieldDefinition()
282302
.name("inputFields")
283-
.type(new GraphQLList(new GraphQLNonNull(__InputValue)))
303+
.type(list(nonNull(__InputValue)))
284304
.dataFetcher(inputFieldsFetcher))
285305
.field(newFieldDefinition()
286306
.name("ofType")
@@ -318,21 +338,21 @@ public enum DirectiveLocation {
318338
.type(GraphQLString))
319339
.field(newFieldDefinition()
320340
.name("locations")
321-
.type(new GraphQLList(new GraphQLNonNull(__DirectiveLocation)))
341+
.type(list(nonNull(__DirectiveLocation)))
322342
.dataFetcher(new DataFetcher() {
323343
@Override
324344
public Object get(DataFetchingEnvironment environment) {
325-
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
345+
GraphQLDirective directive = environment.getSource();
326346
return new ArrayList<DirectiveLocation>(directive.validLocations());
327347
}
328348
}))
329349
.field(newFieldDefinition()
330350
.name("args")
331-
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__InputValue))))
351+
.type(nonNull(list(nonNull(__InputValue))))
332352
.dataFetcher(new DataFetcher() {
333353
@Override
334354
public Object get(DataFetchingEnvironment environment) {
335-
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
355+
GraphQLDirective directive = environment.getSource();
336356
return directive.getArguments();
337357
}
338358
}))
@@ -343,7 +363,7 @@ public Object get(DataFetchingEnvironment environment) {
343363
.dataFetcher(new DataFetcher() {
344364
@Override
345365
public Object get(DataFetchingEnvironment environment) {
346-
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
366+
GraphQLDirective directive = environment.getSource();
347367
return directive.isOnOperation();
348368
}
349369
}))
@@ -354,10 +374,10 @@ public Object get(DataFetchingEnvironment environment) {
354374
.dataFetcher(new DataFetcher() {
355375
@Override
356376
public Object get(DataFetchingEnvironment environment) {
357-
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
377+
GraphQLDirective directive = environment.getSource();
358378
return directive.isOnFragment() ||
359379
(directive.validLocations().contains(DirectiveLocation.INLINE_FRAGMENT)
360-
&& directive.validLocations().contains(DirectiveLocation.FRAGMENT_SPREAD));
380+
&& directive.validLocations().contains(DirectiveLocation.FRAGMENT_SPREAD));
361381
}
362382
}))
363383
.field(newFieldDefinition()
@@ -367,7 +387,7 @@ public Object get(DataFetchingEnvironment environment) {
367387
.dataFetcher(new DataFetcher() {
368388
@Override
369389
public Object get(DataFetchingEnvironment environment) {
370-
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
390+
GraphQLDirective directive = environment.getSource();
371391
return directive.isOnField() ||
372392
directive.validLocations().contains(DirectiveLocation.FIELD);
373393
}
@@ -382,22 +402,22 @@ public Object get(DataFetchingEnvironment environment) {
382402
.field(newFieldDefinition()
383403
.name("types")
384404
.description("A list of all types supported by this server.")
385-
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__Type))))
405+
.type(nonNull(list(nonNull(__Type))))
386406
.dataFetcher(new DataFetcher() {
387407
@Override
388408
public Object get(DataFetchingEnvironment environment) {
389-
GraphQLSchema schema = (GraphQLSchema) environment.getSource();
409+
GraphQLSchema schema = environment.getSource();
390410
return schema.getAllTypesAsList();
391411
}
392412
}))
393413
.field(newFieldDefinition()
394414
.name("queryType")
395415
.description("The type that query operations will be rooted at.")
396-
.type(new GraphQLNonNull(__Type))
416+
.type(nonNull(__Type))
397417
.dataFetcher(new DataFetcher() {
398418
@Override
399419
public Object get(DataFetchingEnvironment environment) {
400-
GraphQLSchema schema = (GraphQLSchema) environment.getSource();
420+
GraphQLSchema schema = environment.getSource();
401421
return schema.getQueryType();
402422
}
403423
}))
@@ -408,14 +428,14 @@ public Object get(DataFetchingEnvironment environment) {
408428
.dataFetcher(new DataFetcher() {
409429
@Override
410430
public Object get(DataFetchingEnvironment environment) {
411-
GraphQLSchema schema = (GraphQLSchema) environment.getSource();
431+
GraphQLSchema schema = environment.getSource();
412432
return schema.getMutationType();
413433
}
414434
}))
415435
.field(newFieldDefinition()
416436
.name("directives")
417437
.description("'A list of all directives supported by this server.")
418-
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__Directive))))
438+
.type(nonNull(list(nonNull(__Directive))))
419439
.dataFetcher(new DataFetcher() {
420440
@Override
421441
public Object get(DataFetchingEnvironment environment) {
@@ -438,7 +458,7 @@ public Object get(DataFetchingEnvironment environment) {
438458

439459
public static GraphQLFieldDefinition SchemaMetaFieldDef = newFieldDefinition()
440460
.name("__schema")
441-
.type(new GraphQLNonNull(__Schema))
461+
.type(nonNull(__Schema))
442462
.description("Access the current type schema of this server.")
443463
.dataFetcher(new DataFetcher() {
444464
@Override
@@ -453,7 +473,7 @@ public Object get(DataFetchingEnvironment environment) {
453473
.description("Request the type information of a single type.")
454474
.argument(newArgument()
455475
.name("name")
456-
.type(new GraphQLNonNull(GraphQLString)))
476+
.type(nonNull(GraphQLString)))
457477
.dataFetcher(new DataFetcher() {
458478
@Override
459479
public Object get(DataFetchingEnvironment environment) {
@@ -464,7 +484,7 @@ public Object get(DataFetchingEnvironment environment) {
464484

465485
public static GraphQLFieldDefinition TypeNameMetaFieldDef = newFieldDefinition()
466486
.name("__typename")
467-
.type(new GraphQLNonNull(GraphQLString))
487+
.type(nonNull(GraphQLString))
468488
.description("The name of the current Object type at runtime.")
469489
.dataFetcher(new DataFetcher() {
470490
@Override

‎src/main/java/graphql/schema/GraphQLList.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88
public class GraphQLList implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLModifiedType, GraphQLNullableType {
99

10+
/**
11+
* A factory method for creating list types so that when used with static imports allows
12+
* more readable code such as
13+
* {@code .type(list(GraphQLString)) }
14+
*
15+
* @param wrappedType the type to wrap as being a list
16+
*
17+
* @return a GraphQLList of that wrapped type
18+
*/
19+
public static GraphQLList list(GraphQLType wrappedType) {
20+
return new GraphQLList(wrappedType);
21+
}
22+
1023
private GraphQLType wrappedType;
1124

1225
public GraphQLList(GraphQLType wrappedType) {

‎src/main/java/graphql/schema/GraphQLNonNull.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
public class GraphQLNonNull implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLModifiedType {
99

10+
/**
11+
* A factory method for creating non null types so that when used with static imports allows
12+
* more readable code such as
13+
* {@code .type(nonNull(GraphQLString)) }
14+
*
15+
* @param wrappedType the type to wrap as being non null
16+
* @return a GraphQLNonNull of that wrapped type
17+
*/
18+
public static GraphQLNonNull nonNull(GraphQLType wrappedType) {
19+
return new GraphQLNonNull(wrappedType);
20+
}
21+
1022
private GraphQLType wrappedType;
1123

1224
public GraphQLNonNull(GraphQLType wrappedType) {

0 commit comments

Comments
 (0)