Skip to content

Commit e74f7f6

Browse files
committed
Merge pull request graphql-java#48 from cardinalraven/bugfix/nested-input-types
Recursively adding input types
2 parents 28f792f + eb03f49 commit e74f7f6

3 files changed

Lines changed: 136 additions & 1 deletion

File tree

src/main/java/graphql/schema/SchemaUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private void collectTypes(GraphQLType root, Map<String, GraphQLType> result) {
5151
} else if (root instanceof GraphQLUnionType) {
5252
collectTypesForUnions((GraphQLUnionType) root, result);
5353
} else if (root instanceof GraphQLInputObjectType) {
54-
result.put(((GraphQLInputObjectType) root).getName(), root);
54+
collectTypesForInputObjects((GraphQLInputObjectType) root, result);
5555
} else if (root instanceof GraphQLTypeReference) {
5656
// nothing to do
5757
} else {
@@ -95,6 +95,16 @@ private void collectTypesForObjects(GraphQLObjectType objectType, Map<String, Gr
9595
}
9696
}
9797

98+
private void collectTypesForInputObjects(GraphQLInputObjectType objectType, Map<String, GraphQLType> result) {
99+
if (result.containsKey(objectType.getName())) return;
100+
result.put(objectType.getName(), objectType);
101+
102+
for (GraphQLInputObjectField fieldDefinition : objectType.getFields()) {
103+
collectTypes(fieldDefinition.getType(), result);
104+
}
105+
}
106+
107+
98108
public Map<String, GraphQLType> allTypes(GraphQLSchema schema, Set<GraphQLType> dictionary) {
99109
Map<String, GraphQLType> typesByName = new LinkedHashMap<>();
100110
collectTypes(schema.getQueryType(), typesByName);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package graphql;
2+
3+
import graphql.schema.DataFetcher;
4+
import graphql.schema.DataFetchingEnvironment;
5+
import graphql.schema.GraphQLArgument;
6+
import graphql.schema.GraphQLFieldDefinition;
7+
import graphql.schema.GraphQLInputObjectField;
8+
import graphql.schema.GraphQLInputObjectType;
9+
import graphql.schema.GraphQLObjectType;
10+
import graphql.schema.GraphQLSchema;
11+
import java.util.Map;
12+
import static graphql.Scalars.GraphQLBoolean;
13+
import static graphql.Scalars.GraphQLInt;
14+
15+
public class NestedInputSchema {
16+
17+
18+
public static GraphQLSchema createSchema() {
19+
20+
21+
GraphQLObjectType root = rootType();
22+
23+
return GraphQLSchema.newSchema()
24+
.query(root)
25+
.build();
26+
}
27+
28+
public static GraphQLObjectType rootType() {
29+
return GraphQLObjectType.newObject()
30+
31+
.name("Root")
32+
.field(GraphQLFieldDefinition.newFieldDefinition()
33+
.name("value")
34+
.type(GraphQLInt)
35+
.dataFetcher(new DataFetcher() {
36+
@Override
37+
public Object get(DataFetchingEnvironment environment) {
38+
int initialValue = environment.getArgument("initialValue");
39+
Map<String, Object> filter = environment.getArgument("filter");
40+
if (filter != null) {
41+
if (filter.containsKey("even")) {
42+
Boolean even = (Boolean) filter.get("even");
43+
if (even && (initialValue%2 != 0)) {
44+
return 0;
45+
} else if (!even && (initialValue%2 == 0)) {
46+
return 0;
47+
}
48+
}
49+
if (filter.containsKey("range")) {
50+
Map<String, Integer> range = (Map<String, Integer>) filter.get("range");
51+
if (initialValue < range.get("lowerBound") ||
52+
initialValue > range.get("upperBound")) {
53+
return 0;
54+
}
55+
}
56+
}
57+
return initialValue;
58+
}})
59+
.argument(GraphQLArgument.newArgument()
60+
.name("intialValue")
61+
.type(GraphQLInt)
62+
.defaultValue(5)
63+
.build())
64+
.argument(GraphQLArgument.newArgument()
65+
.name("filter")
66+
.type(filterType())
67+
.build())
68+
.build())
69+
.build();
70+
}
71+
72+
public static GraphQLInputObjectType filterType() {
73+
return GraphQLInputObjectType.newInputObject()
74+
.name("Filter")
75+
.field(GraphQLInputObjectField.newInputObjectField()
76+
.name("even")
77+
.type(GraphQLBoolean)
78+
.build())
79+
.field(GraphQLInputObjectField.newInputObjectField()
80+
.name("range")
81+
.type(rangeType())
82+
.build())
83+
.build();
84+
}
85+
86+
public static GraphQLInputObjectType rangeType() {
87+
return GraphQLInputObjectType.newInputObject()
88+
.name("Range")
89+
.field(GraphQLInputObjectField.newInputObjectField()
90+
.name("lowerBound")
91+
.type(GraphQLInt)
92+
.build())
93+
.field(GraphQLInputObjectField.newInputObjectField()
94+
.name("upperBound")
95+
.type(GraphQLInt)
96+
.build())
97+
.build();
98+
}
99+
}

src/test/groovy/graphql/schema/SchemaUtilTest.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package graphql.schema
22

3+
import graphql.NestedInputSchema
4+
import graphql.Scalars
35
import graphql.introspection.Introspection
46
import spock.lang.Specification
57

68
import java.util.Collections;
79

810
import static graphql.Scalars.GraphQLBoolean
11+
import static graphql.Scalars.GraphQLInt
912
import static graphql.Scalars.GraphQLString
1013
import static graphql.StarWarsSchema.*
1114

@@ -30,4 +33,27 @@ class SchemaUtilTest extends Specification {
3033
(Introspection.__Directive.name) : Introspection.__Directive,
3134
(GraphQLBoolean.name) : GraphQLBoolean]
3235
}
36+
37+
def "collectAllTypesNestedInput"() {
38+
when:
39+
Map<String, GraphQLType> types = new SchemaUtil().allTypes(NestedInputSchema.createSchema(), Collections.emptySet());
40+
Map<String, GraphQLType> expected =
41+
42+
[(NestedInputSchema.rootType().name) : NestedInputSchema.rootType(),
43+
(NestedInputSchema.filterType().name) : NestedInputSchema.filterType(),
44+
(NestedInputSchema.rangeType().name) : NestedInputSchema.rangeType(),
45+
(GraphQLInt.name) : GraphQLInt,
46+
(GraphQLString.name) : GraphQLString,
47+
(Introspection.__Schema.name) : Introspection.__Schema,
48+
(Introspection.__Type.name) : Introspection.__Type,
49+
(Introspection.__TypeKind.name) : Introspection.__TypeKind,
50+
(Introspection.__Field.name) : Introspection.__Field,
51+
(Introspection.__InputValue.name): Introspection.__InputValue,
52+
(Introspection.__EnumValue.name) : Introspection.__EnumValue,
53+
(Introspection.__Directive.name) : Introspection.__Directive,
54+
(GraphQLBoolean.name) : GraphQLBoolean];
55+
then:
56+
types.keySet() == expected.keySet()
57+
}
58+
3359
}

0 commit comments

Comments
 (0)