Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/graphql/schema/SchemaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void collectTypes(GraphQLType root, Map<String, GraphQLType> result) {
} else if (root instanceof GraphQLUnionType) {
collectTypesForUnions((GraphQLUnionType) root, result);
} else if (root instanceof GraphQLInputObjectType) {
result.put(((GraphQLInputObjectType) root).getName(), root);
collectTypesForInputObjects((GraphQLInputObjectType) root, result);
} else if (root instanceof GraphQLTypeReference) {
// nothing to do
} else {
Expand Down Expand Up @@ -95,6 +95,16 @@ private void collectTypesForObjects(GraphQLObjectType objectType, Map<String, Gr
}
}

private void collectTypesForInputObjects(GraphQLInputObjectType objectType, Map<String, GraphQLType> result) {
if (result.containsKey(objectType.getName())) return;
result.put(objectType.getName(), objectType);

for (GraphQLInputObjectField fieldDefinition : objectType.getFields()) {
collectTypes(fieldDefinition.getType(), result);
}
}


public Map<String, GraphQLType> allTypes(GraphQLSchema schema, Set<GraphQLType> dictionary) {
Map<String, GraphQLType> typesByName = new LinkedHashMap<>();
collectTypes(schema.getQueryType(), typesByName);
Expand Down
99 changes: 99 additions & 0 deletions src/test/groovy/graphql/NestedInputSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
26 changes: 26 additions & 0 deletions src/test/groovy/graphql/schema/SchemaUtilTest.groovy
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package graphql.schema

import graphql.NestedInputSchema
import graphql.Scalars
import graphql.introspection.Introspection
import spock.lang.Specification

import java.util.Collections;

import static graphql.Scalars.GraphQLBoolean
import static graphql.Scalars.GraphQLInt
import static graphql.Scalars.GraphQLString
import static graphql.StarWarsSchema.*

Expand All @@ -30,4 +33,27 @@ class SchemaUtilTest extends Specification {
(Introspection.__Directive.name) : Introspection.__Directive,
(GraphQLBoolean.name) : GraphQLBoolean]
}

def "collectAllTypesNestedInput"() {
when:
Map<String, GraphQLType> types = new SchemaUtil().allTypes(NestedInputSchema.createSchema(), Collections.emptySet());
Map<String, GraphQLType> expected =

[(NestedInputSchema.rootType().name) : NestedInputSchema.rootType(),
(NestedInputSchema.filterType().name) : NestedInputSchema.filterType(),
(NestedInputSchema.rangeType().name) : NestedInputSchema.rangeType(),
(GraphQLInt.name) : GraphQLInt,
(GraphQLString.name) : GraphQLString,
(Introspection.__Schema.name) : Introspection.__Schema,
(Introspection.__Type.name) : Introspection.__Type,
(Introspection.__TypeKind.name) : Introspection.__TypeKind,
(Introspection.__Field.name) : Introspection.__Field,
(Introspection.__InputValue.name): Introspection.__InputValue,
(Introspection.__EnumValue.name) : Introspection.__EnumValue,
(Introspection.__Directive.name) : Introspection.__Directive,
(GraphQLBoolean.name) : GraphQLBoolean];
then:
types.keySet() == expected.keySet()
}

}