Skip to content

Commit 12dc200

Browse files
authored
fix: Support Enum types in EQ, NE, IN, NIN criteria expressions (#50)
* fix: ignore .classpath * fix: add support for Enum predicate builder expressions: EQ, NE, IN, NIN
1 parent 6407149 commit 12dc200

File tree

7 files changed

+147
-177
lines changed

7 files changed

+147
-177
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ target/
2323
.springBeans
2424

2525

26+
27+
graphql-jpa-query-schema/\.classpath
28+
29+
graphql-jpa-query-annotations/\.classpath
30+
31+
graphql-jpa-query-example/\.classpath
32+
33+
graphql-jpa-query-boot-starter/\.classpath

graphql-jpa-query-annotations/.classpath

Lines changed: 0 additions & 42 deletions
This file was deleted.

graphql-jpa-query-boot-starter/.classpath

Lines changed: 0 additions & 47 deletions
This file was deleted.

graphql-jpa-query-example/.classpath

Lines changed: 0 additions & 42 deletions
This file was deleted.

graphql-jpa-query-schema/.classpath

Lines changed: 0 additions & 42 deletions
This file was deleted.

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Set;
2525
import java.util.UUID;
2626

27-
2827
import javax.persistence.criteria.CriteriaBuilder;
2928
import javax.persistence.criteria.Expression;
3029
import javax.persistence.criteria.From;
@@ -286,6 +285,34 @@ private Predicate getUuidPredicate(Path<UUID> field, PredicateFilter filter) {
286285
}
287286
return null;
288287
}
288+
289+
private Predicate getEnumPredicate(Path<? extends Enum> field, PredicateFilter filter) {
290+
if (filter.getValue() == null) {
291+
return null;
292+
}
293+
final Predicate arrayPredicate = mayBeArrayValuePredicate(field, filter);
294+
295+
if (arrayPredicate != null) {
296+
return arrayPredicate;
297+
}
298+
final Enum<?> compareValue = (Enum<?>) filter.getValue();
299+
300+
if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
301+
return cb.equal(field, compareValue);
302+
}
303+
if (filter.getCriterias().contains(PredicateFilter.Criteria.IN)) {
304+
final CriteriaBuilder.In<Object> in = cb.in(field);
305+
return in.value(compareValue);
306+
}
307+
if (filter.getCriterias().contains(PredicateFilter.Criteria.NIN)) {
308+
final CriteriaBuilder.In<Object> in = cb.in(field);
309+
return cb.not(in.value(compareValue));
310+
}
311+
if (filter.getCriterias().contains(PredicateFilter.Criteria.NE)) {
312+
return cb.notEqual(field, compareValue);
313+
}
314+
return null;
315+
}
289316

290317
@SuppressWarnings("unchecked")
291318
private Predicate getTypedPredicate(From<?,?> from, Path<?> field, PredicateFilter filter) {
@@ -334,6 +361,8 @@ else if (type.equals(UUID.class)) {
334361
else if(Collection.class.isAssignableFrom(type)) {
335362
if(field.getModel() == null)
336363
return from.join(filter.getField()).in(value);
364+
} else if(type.isEnum()) {
365+
return getEnumPredicate((Path<Enum<?>>) field, predicateFilter);
337366
}
338367

339368
throw new IllegalArgumentException("Unsupported field type " + type + " for field " + predicateFilter.getField());

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

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import java.util.HashMap;
22-
import java.util.UUID;
2322

2423
import javax.persistence.EntityManager;
2524

@@ -57,10 +56,8 @@ public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManag
5756

5857
}
5958

60-
6159
@Autowired
6260
private GraphQLExecutor executor;
63-
6461

6562
@Test
6663
public void contextLoads() {
@@ -222,6 +219,115 @@ public void queryForElementCollection() {
222219
assertThat(result.toString()).isEqualTo(expected);
223220
}
224221

222+
@Test
223+
public void queryForEnumIn() {
224+
//given
225+
String query = "{ Books(where: {genre: {IN: PLAY}}) { select { id title, genre } }}";
226+
227+
String expected = "{Books={select=["
228+
+ "{id=5, title=The Cherry Orchard, genre=PLAY}, "
229+
+ "{id=6, title=The Seagull, genre=PLAY}, "
230+
+ "{id=7, title=Three Sisters, genre=PLAY}"
231+
+ "]}}";
232+
233+
//when
234+
Object result = executor.execute(query).getData();
235+
236+
// then
237+
assertThat(result.toString()).isEqualTo(expected);
238+
}
239+
240+
@Test
241+
public void queryForEnumInArray() {
242+
//given
243+
String query = "{ Books(where: {genre: {IN: [NOVEL, PLAY]}}) { select { id title, genre } }}";
244+
245+
String expected = "{Books={select=["
246+
+ "{id=2, title=War and Peace, genre=NOVEL}, "
247+
+ "{id=3, title=Anna Karenina, genre=NOVEL}, "
248+
+ "{id=5, title=The Cherry Orchard, genre=PLAY}, "
249+
+ "{id=6, title=The Seagull, genre=PLAY}, "
250+
+ "{id=7, title=Three Sisters, genre=PLAY}"
251+
+ "]}}";
252+
253+
//when
254+
Object result = executor.execute(query).getData();
255+
256+
// then
257+
assertThat(result.toString()).isEqualTo(expected);
258+
}
259+
260+
@Test
261+
public void queryForEnumNinArray() {
262+
//given
263+
String query = "{ Books(where: {genre: {NIN: [NOVEL]}}) { select { id title, genre } }}";
264+
265+
String expected = "{Books={select=["
266+
+ "{id=5, title=The Cherry Orchard, genre=PLAY}, "
267+
+ "{id=6, title=The Seagull, genre=PLAY}, "
268+
+ "{id=7, title=Three Sisters, genre=PLAY}"
269+
+ "]}}";
270+
271+
//when
272+
Object result = executor.execute(query).getData();
273+
274+
// then
275+
assertThat(result.toString()).isEqualTo(expected);
276+
}
277+
278+
@Test
279+
public void queryForEnumEq() {
280+
//given
281+
String query = "{ Books(where: {genre: {EQ: NOVEL}}) { select { id title, genre } }}";
282+
283+
String expected = "{Books={select=["
284+
+ "{id=2, title=War and Peace, genre=NOVEL}, "
285+
+ "{id=3, title=Anna Karenina, genre=NOVEL}"
286+
+ "]}}";
287+
288+
//when
289+
Object result = executor.execute(query).getData();
290+
291+
// then
292+
assertThat(result.toString()).isEqualTo(expected);
293+
}
294+
295+
@Test
296+
public void queryForEnumNe() {
297+
//given
298+
String query = "{ Books(where: {genre: {NE: PLAY}}) { select { id title, genre } }}";
299+
300+
String expected = "{Books={select=["
301+
+ "{id=2, title=War and Peace, genre=NOVEL}, "
302+
+ "{id=3, title=Anna Karenina, genre=NOVEL}"
303+
+ "]}}";
304+
305+
//when
306+
Object result = executor.execute(query).getData();
307+
308+
// then
309+
assertThat(result.toString()).isEqualTo(expected);
310+
}
311+
312+
@Test
313+
public void queryForEnumNin() {
314+
//given
315+
String query = "{ Books(where: {genre: {NIN: PLAY}}) { select { id title, genre } }}";
316+
317+
String expected = "{Books={select=["
318+
+ "{id=2, title=War and Peace, genre=NOVEL}, "
319+
+ "{id=3, title=Anna Karenina, genre=NOVEL}"
320+
+ "]}}";
321+
322+
//when
323+
Object result = executor.execute(query).getData();
324+
325+
// then
326+
assertThat(result.toString()).isEqualTo(expected);
327+
}
328+
329+
330+
225331
// https://github.com/introproventures/graphql-jpa-query/issues/30
226332
@Test
227333
public void queryForEntityWithMappedSuperclass() {

0 commit comments

Comments
 (0)