Skip to content

Commit cdedf3b

Browse files
author
Bojan Tomic
committed
graphql-java#172 Allow TypeReference as InputType
1 parent 0946f83 commit cdedf3b

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

src/main/java/graphql/schema/GraphQLArgument.java

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

33

4+
import java.util.Map;
5+
46
import static graphql.Assert.assertNotNull;
57

68
public class GraphQLArgument {
@@ -24,6 +26,10 @@ public GraphQLArgument(String name, GraphQLInputType type) {
2426
}
2527

2628

29+
void replaceTypeReferences(Map<String, GraphQLType> typeMap) {
30+
type = (GraphQLInputType) new SchemaUtil().resolveTypeReference(type, typeMap);
31+
}
32+
2733
public String getName() {
2834
return name;
2935
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package graphql.schema;
2+
3+
import java.util.List;
4+
5+
public interface GraphQLInputFieldsContainer extends GraphQLType {
6+
7+
GraphQLInputObjectField getFieldDefinition(String name);
8+
9+
List<GraphQLInputObjectField> getFieldDefinitions();
10+
}

src/main/java/graphql/schema/GraphQLInputObjectField.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package graphql.schema;
22

33

4+
import java.util.Map;
5+
46
import static graphql.Assert.assertNotNull;
57

68
public class GraphQLInputObjectField {
@@ -23,6 +25,9 @@ public GraphQLInputObjectField(String name, String description, GraphQLInputType
2325
this.description = description;
2426
}
2527

28+
void replaceTypeReferences(Map<String, GraphQLType> typeMap) {
29+
type = (GraphQLInputType) new SchemaUtil().resolveTypeReference(type, typeMap);
30+
}
2631

2732
public String getName() {
2833
return name;

src/main/java/graphql/schema/GraphQLInputObjectType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package graphql.schema;
22

3-
import graphql.AssertException;
4-
53
import java.util.ArrayList;
64
import java.util.LinkedHashMap;
75
import java.util.List;
86
import java.util.Map;
97

8+
import graphql.AssertException;
9+
1010
import static graphql.Assert.assertNotNull;
1111

12-
public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, GraphQLUnmodifiedType, GraphQLNullableType {
12+
public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, GraphQLUnmodifiedType, GraphQLNullableType, GraphQLInputFieldsContainer {
1313

1414
private final String name;
1515
private final String description;
@@ -54,6 +54,16 @@ public static Builder newInputObject() {
5454
return new Builder();
5555
}
5656

57+
@Override
58+
public GraphQLInputObjectField getFieldDefinition(String name) {
59+
return fieldMap.get(name);
60+
}
61+
62+
@Override
63+
public List<GraphQLInputObjectField> getFieldDefinitions() {
64+
return new ArrayList<GraphQLInputObjectField>(fieldMap.values());
65+
}
66+
5767
public static class Builder {
5868
private String name;
5969
private String description;

src/main/java/graphql/schema/GraphQLTypeReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* A special type to allow a object/interface types to reference itself. It's replaced with the real type
88
* object when the schema is build.
99
*/
10-
public class GraphQLTypeReference implements GraphQLType, GraphQLOutputType {
10+
public class GraphQLTypeReference implements GraphQLType, GraphQLOutputType, GraphQLInputType {
1111

1212
private final String name;
1313

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package graphql.schema;
22

33

4+
import java.util.ArrayList;
5+
import java.util.LinkedHashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
410
import graphql.GraphQLException;
511
import graphql.introspection.Introspection;
612

7-
import java.util.*;
8-
913
public class SchemaUtil {
1014

1115
public boolean isLeafType(GraphQLType type) {
@@ -37,9 +41,9 @@ private void collectTypes(GraphQLType root, Map<String, GraphQLType> result) {
3741
} else if (root instanceof GraphQLList) {
3842
collectTypes(((GraphQLList) root).getWrappedType(), result);
3943
} else if (root instanceof GraphQLEnumType) {
40-
result.put(((GraphQLEnumType) root).getName(), root);
44+
result.put(root.getName(), root);
4145
} else if (root instanceof GraphQLScalarType) {
42-
result.put(((GraphQLScalarType) root).getName(), root);
46+
result.put(root.getName(), root);
4347
} else if (root instanceof GraphQLObjectType) {
4448
collectTypesForObjects((GraphQLObjectType) root, result);
4549
} else if (root instanceof GraphQLInterfaceType) {
@@ -136,12 +140,24 @@ void replaceTypeReferences(GraphQLSchema schema) {
136140
if (type instanceof GraphQLFieldsContainer) {
137141
resolveTypeReferencesForFieldsContainer((GraphQLFieldsContainer) type, typeMap);
138142
}
143+
if (type instanceof GraphQLInputFieldsContainer) {
144+
resolveTypeReferencesForInputFieldsContainer((GraphQLInputFieldsContainer) type, typeMap);
145+
}
139146
}
140147
}
141148

142149
private void resolveTypeReferencesForFieldsContainer(GraphQLFieldsContainer fieldsContainer, Map<String, GraphQLType> typeMap) {
143150
for (GraphQLFieldDefinition fieldDefinition : fieldsContainer.getFieldDefinitions()) {
144151
fieldDefinition.replaceTypeReferences(typeMap);
152+
for (GraphQLArgument argument : fieldDefinition.getArguments()) {
153+
argument.replaceTypeReferences(typeMap);
154+
}
155+
}
156+
}
157+
158+
private void resolveTypeReferencesForInputFieldsContainer(GraphQLInputFieldsContainer fieldsContainer, Map<String, GraphQLType> typeMap) {
159+
for (GraphQLInputObjectField fieldDefinition : fieldsContainer.getFieldDefinitions()) {
160+
fieldDefinition.replaceTypeReferences(typeMap);
145161
}
146162
}
147163

0 commit comments

Comments
 (0)