forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedInputSchema.java
More file actions
99 lines (88 loc) · 4.05 KB
/
NestedInputSchema.java
File metadata and controls
99 lines (88 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package graphql;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import java.util.Map;
import static graphql.Scalars.GraphQLBoolean;
import static graphql.Scalars.GraphQLInt;
public class NestedInputSchema {
public static GraphQLSchema createSchema() {
GraphQLObjectType root = rootType();
return GraphQLSchema.newSchema()
.query(root)
.build();
}
public static GraphQLObjectType rootType() {
return GraphQLObjectType.newObject()
.name("Root")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("value")
.type(GraphQLInt)
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
int initialValue = environment.getArgument("initialValue");
Map<String, Object> filter = environment.getArgument("filter");
if (filter != null) {
if (filter.containsKey("even")) {
Boolean even = (Boolean) filter.get("even");
if (even && (initialValue%2 != 0)) {
return 0;
} else if (!even && (initialValue%2 == 0)) {
return 0;
}
}
if (filter.containsKey("range")) {
Map<String, Integer> range = (Map<String, Integer>) filter.get("range");
if (initialValue < range.get("lowerBound") ||
initialValue > range.get("upperBound")) {
return 0;
}
}
}
return initialValue;
}})
.argument(GraphQLArgument.newArgument()
.name("intialValue")
.type(GraphQLInt)
.defaultValue(5)
.build())
.argument(GraphQLArgument.newArgument()
.name("filter")
.type(filterType())
.build())
.build())
.build();
}
public static GraphQLInputObjectType filterType() {
return GraphQLInputObjectType.newInputObject()
.name("Filter")
.field(GraphQLInputObjectField.newInputObjectField()
.name("even")
.type(GraphQLBoolean)
.build())
.field(GraphQLInputObjectField.newInputObjectField()
.name("range")
.type(rangeType())
.build())
.build();
}
public static GraphQLInputObjectType rangeType() {
return GraphQLInputObjectType.newInputObject()
.name("Range")
.field(GraphQLInputObjectField.newInputObjectField()
.name("lowerBound")
.type(GraphQLInt)
.build())
.field(GraphQLInputObjectField.newInputObjectField()
.name("upperBound")
.type(GraphQLInt)
.build())
.build();
}
}