Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5feb86

Browse files
committedDec 27, 2023
Backport fix variable value conversion for array of values (#428)
1 parent 7a6645c commit f5feb86

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed
 

‎schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaQueryFactory.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import graphql.language.StringValue;
5454
import graphql.language.Value;
5555
import graphql.language.VariableReference;
56+
import graphql.schema.Coercing;
5657
import graphql.schema.DataFetchingEnvironment;
5758
import graphql.schema.GraphQLArgument;
5859
import graphql.schema.GraphQLFieldDefinition;
@@ -1507,22 +1508,30 @@ protected Object convertValue(DataFetchingEnvironment environment, Argument argu
15071508
// Return provided StringValue
15081509
return ((StringValue) value).getValue();
15091510
}
1510-
} else if (value instanceof VariableReference) {
1511+
} else if (value instanceof VariableReference variableReference) {
15111512
Class javaType = getJavaType(environment, argument);
1512-
Object argumentValue = environment.getVariables().get(VariableReference.class.cast(value).getName());
1513+
Object argumentValue = environment.getVariables().get(variableReference.getName());
1514+
15131515
if (javaType.isEnum()) {
1514-
if (argumentValue instanceof Collection) {
1516+
if (argumentValue instanceof Collection argumentValues) {
15151517
List<Enum> values = new ArrayList<>();
15161518

1517-
Collection.class.cast(argumentValue)
1518-
.forEach(it -> values.add(Enum.valueOf(javaType, it.toString())));
1519+
argumentValues.forEach(it -> values.add(Enum.valueOf(javaType, it.toString())));
1520+
15191521
return values;
15201522
} else {
15211523
return Enum.valueOf(javaType, argumentValue.toString());
15221524
}
15231525
} else {
1524-
// Get resolved variable in environment arguments
1525-
return argumentValue;
1526+
Coercing<?, ?> coercing = JavaScalars.of(javaType).getCoercing();
1527+
Function<Object, Object> valueConverter = it -> javaType.isInstance(it) ? it : coercing.parseValue(it);
1528+
1529+
if (argumentValue instanceof Collection<?> argumentValues) {
1530+
return argumentValues.stream().map(valueConverter).toList();
1531+
} else {
1532+
// Get resolved variable in environment arguments
1533+
return valueConverter.apply(argumentValue);
1534+
}
15261535
}
15271536
} else if (value instanceof ArrayValue) {
15281537
Collection arrayValue = environment.getArgument(argument.getName());

‎schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ private Predicate getTypedPredicate(From<?, ?> from, Path<?> field, PredicateFil
794794
Class<?> type = filter.getJavaType();
795795
Object value = filter.getValue();
796796
Set<Criteria> criterias = filter.getCriterias();
797-
Attribute attribute = filter.getAttribute();
797+
Attribute<?, ?> attribute = filter.getAttribute();
798798

799799
if (value == null) {
800800
return cb.disjunction();

‎schema/src/test/java/com/introproventures/graphql/jpa/query/schema/GraphQLExecutorTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,25 @@ public void queryForThingByIdViaWhereIN() {
195195
assertThat(result.toString()).isEqualTo(expected);
196196
}
197197

198+
@Test
199+
public void queryForThingByIdViaWhereINVariables() {
200+
//given:
201+
202+
String query = "query things($ids: [UUID]!){ Things(where: {id: {IN: $ids}}) { select { id }}}";
203+
String expected = "{Things={select=[{id=2d1ebc5b-7d27-4197-9cf0-e84451c5bbb1}]}}";
204+
205+
//when:
206+
Object result = executor
207+
.execute(
208+
query,
209+
Map.of("ids", List.of("2d1ebc5b-7d27-4197-9cf0-e84451c5bbb1", "2d1ebc5b-7d27-4197-9cf0-e84451c5bbb1"))
210+
)
211+
.getData();
212+
213+
//then:
214+
assertThat(result.toString()).isEqualTo(expected);
215+
}
216+
198217
@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
199218
@Test
200219
public void queryForThingByIdViaWhereNE() {

0 commit comments

Comments
 (0)
Please sign in to comment.