Skip to content

Commit 026068d

Browse files
authored
Fix where query criteria with multiple associations joins (#485)
1 parent 35975f1 commit 026068d

File tree

3 files changed

+105
-6
lines changed

3 files changed

+105
-6
lines changed

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -1108,12 +1108,23 @@ protected Predicate getObjectFieldPredicate(
11081108

11091109
AbstractQuery<?> query = environment.getRoot();
11101110
Boolean isFetch = environment.getLocalContext();
1111-
Boolean isOptional = isOptionalAttribute(attribute);
1112-
1113-
From<?, ?> context = (isSubquery(query) || isCountQuery(query) || !isFetch)
1114-
? reuseJoin(from, objectField.getName(), isOptional)
1115-
: reuseFetch(from, objectField.getName(), isOptional);
1116-
1111+
boolean isOptional = isOptionalAttribute(attribute);
1112+
List<Map<String, Object>> logicalArguments = Optional
1113+
.ofNullable(environment.getArgument(logical.name()))
1114+
.filter(List.class::isInstance)
1115+
.map(List.class::cast)
1116+
.orElseGet(List::of);
1117+
1118+
From<?, ?> context;
1119+
if (logicalArguments.stream().filter(it -> it.containsKey(objectField.getName())).count() > 1) {
1120+
context =
1121+
isOptional ? from.join(objectField.getName(), JoinType.LEFT) : from.join(objectField.getName());
1122+
} else {
1123+
context =
1124+
(isSubquery(query) || isCountQuery(query) || !isFetch)
1125+
? reuseJoin(from, objectField.getName(), isOptional)
1126+
: reuseFetch(from, objectField.getName(), isOptional);
1127+
}
11171128
return getArgumentPredicate(
11181129
cb,
11191130
context,

schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaConverterTests.java

+81
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,42 @@ public void criteriaTester3() {
179179
assertThat(result).hasSize(1);
180180
}
181181

182+
@Test
183+
@Transactional
184+
public void criteriaTesterMultipleJoinWhereCriteria() {
185+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
186+
187+
CriteriaQuery<TaskEntity> tasksQuery = cb.createQuery(TaskEntity.class);
188+
Root<TaskEntity> task = tasksQuery.from(TaskEntity.class);
189+
190+
Join<TaskEntity, TaskVariableEntity> taskVariableEntityJoin1 = task.join("variables");
191+
192+
Predicate var1 = cb.and(
193+
cb.equal(taskVariableEntityJoin1.get("name"), "variable2"),
194+
cb.equal(taskVariableEntityJoin1.get("value"), new VariableValue<>(Boolean.TRUE))
195+
);
196+
197+
taskVariableEntityJoin1.on(var1);
198+
taskVariableEntityJoin1.alias("var1");
199+
200+
Join<TaskEntity, TaskVariableEntity> taskVariableEntityJoin2 = task.join("variables");
201+
Predicate var2 = cb.and(
202+
cb.equal(taskVariableEntityJoin2.get("name"), "variable1"),
203+
cb.equal(taskVariableEntityJoin2.get("value"), new VariableValue<>(new String("data")))
204+
);
205+
206+
taskVariableEntityJoin2.on(var2);
207+
taskVariableEntityJoin2.alias("var2");
208+
209+
tasksQuery.select(task);
210+
// when:
211+
List<TaskEntity> result = entityManager.createQuery(tasksQuery).getResultList();
212+
213+
// then:
214+
assertThat(result).isNotEmpty();
215+
assertThat(result).hasSize(1);
216+
}
217+
182218
@Test
183219
@Transactional
184220
public void criteriaTester4() {
@@ -838,6 +874,51 @@ public void queryTasksVariablesWhereWithExplicitANDEXISTSByNameAndValueCriteria(
838874
assertThat(result.toString()).isEqualTo(expected);
839875
}
840876

877+
@Test
878+
public void queryTasksVariablesWhereWithExplicitANDByMultipleNameAndValueCriteria() {
879+
//given
880+
String query =
881+
"query {" +
882+
" Tasks(where: {" +
883+
" status: {EQ: COMPLETED}" +
884+
" AND: [" +
885+
" {" +
886+
" variables: {" +
887+
" name: {EQ: \"variable1\"}" +
888+
" value: {EQ: \"data\"} }" +
889+
" }" +
890+
" {" +
891+
" variables: {" +
892+
" name: {EQ: \"variable2\"}" +
893+
" value: {EQ: true} }" +
894+
" }" +
895+
" ]" +
896+
" }) {" +
897+
" select {" +
898+
" id" +
899+
" status" +
900+
" variables {" +
901+
" name" +
902+
" value" +
903+
" }" +
904+
" }" +
905+
" }" +
906+
"}";
907+
908+
String expected =
909+
"{Tasks={select=[" +
910+
"{id=1, status=COMPLETED, variables=[" +
911+
"{name=variable2, value=true}, " +
912+
"{name=variable1, value=data}]}" +
913+
"]}}";
914+
915+
//when
916+
Object result = executor.execute(query).getData();
917+
918+
// then
919+
assertThat(result.toString()).isEqualTo(expected);
920+
}
921+
841922
@Test
842923
public void queryTasksVariablesWhereWithEXISTSByNameAndValueCriteria() {
843924
//given

tests/gatling/src/main/resources/data.sql

+7
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,10 @@ insert into INTEGRATION_CONTEXT (id, client_id, client_name, execution_id, proce
10901090
('2', '1', 'serviceTask', '2', '1'),
10911091
('3', '1', 'serviceTask', '3', '1');
10921092

1093+
insert into TASK_PROCESS_VARIABLE (task_id, process_variable_id) values
1094+
(1,1),
1095+
(1,2),
1096+
(1,3),
1097+
(1,4),
1098+
(1,5),
1099+
(1,6);

0 commit comments

Comments
 (0)