Skip to content

Commit 1414bc0

Browse files
committed
Problem: passing input object as a resolved class doesn't work
ValueResolver makes an assumption that an object passed through is always going to be a map. However, when using graphql-java with graphql-java-annotations and graphql-java-servlet (GraphQLVariables), the inputs passed through are already resolved. Solution: this patch allows to pass such objects through the resolver unmodified, without throwing an exception.
1 parent d6ef614 commit 1414bc0

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎src/main/java/graphql/execution/ValuesResolver.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ private Object coerceValue(GraphQLType graphQLType, Object value) {
8080
return coerceValueForEnum((GraphQLEnumType) graphQLType, value);
8181
} else if (graphQLType instanceof GraphQLList) {
8282
return coerceValueForList((GraphQLList) graphQLType, value);
83-
} else if (graphQLType instanceof GraphQLInputObjectType) {
83+
} else if (graphQLType instanceof GraphQLInputObjectType && value instanceof Map) {
8484
return coerceValueForInputObjectType((GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
85+
} else if (graphQLType instanceof GraphQLInputObjectType) {
86+
return value;
8587
} else {
8688
throw new GraphQLException("unknown type " + graphQLType);
8789
}

‎src/test/groovy/graphql/execution/ValuesResolverTest.groovy‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ValuesResolverTest extends Specification {
3838

3939
}
4040

41-
def "getVariableValues: object as variable input"() {
41+
def "getVariableValues: map object as variable input"() {
4242
given:
4343
def nameField = newInputObjectField()
4444
.name("name")
@@ -60,6 +60,41 @@ class ValuesResolverTest extends Specification {
6060
resolvedValues['variable'] == [name: 'a', id: 123]
6161
}
6262

63+
64+
class Person {
65+
def name = ""
66+
def id = 0
67+
68+
Person(name, id) {
69+
this.name = name
70+
this.id = id
71+
}
72+
73+
}
74+
75+
def "getVariableValues: object as variable input"() {
76+
given:
77+
def nameField = newInputObjectField()
78+
.name("name")
79+
.type(GraphQLString)
80+
def idField = newInputObjectField()
81+
.name("id")
82+
.type(GraphQLInt)
83+
def inputType = newInputObject()
84+
.name("Person")
85+
.field(nameField)
86+
.field(idField)
87+
.build()
88+
def schema = TestUtil.schemaWithInputType(inputType)
89+
VariableDefinition variableDefinition = new VariableDefinition("variable", new TypeName("Person"))
90+
91+
when:
92+
def obj = new Person('a', 123)
93+
def resolvedValues = resolver.getVariableValues(schema, [variableDefinition], [variable: obj])
94+
then:
95+
resolvedValues['variable'] == obj
96+
}
97+
6398
def "getVariableValues: simple value gets resolved to a list when the type is a List"() {
6499
given:
65100
def schema = TestUtil.schemaWithInputType(new GraphQLList(GraphQLString))

0 commit comments

Comments
 (0)