Skip to content

Commit ac7bda6

Browse files
authored
feat: add IN | NIN predicate for custom attribute types (#136)
* feat: add IN | NIN predicate for custom attribute types * fix: add more tests
1 parent 6359449 commit ac7bda6

File tree

2 files changed

+187
-12
lines changed

2 files changed

+187
-12
lines changed

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

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.lang.reflect.Constructor;
2020
import java.math.BigDecimal;
2121
import java.math.BigInteger;
22+
import java.util.ArrayList;
2223
import java.util.Collection;
2324
import java.util.Date;
2425
import java.util.EnumSet;
26+
import java.util.List;
2527
import java.util.Set;
2628
import java.util.UUID;
2729

@@ -420,26 +422,66 @@ else if (Object.class.isAssignableFrom(type)) {
420422
if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
421423
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
422424
}
423-
else if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
425+
else {
424426
Object object = value;
425427

426-
try {
427-
Constructor<?> constructor = type.getConstructor(Object.class);
428-
if(constructor != null) {
429-
Object arg = NullValue.class.isInstance(value) ? null : value;
430-
object = constructor.newInstance(arg);
431-
}
432-
} catch (Exception e) {
433-
e.printStackTrace();
428+
if(List.class.isInstance(value)) {
429+
object = getValues(object, type);
430+
} else {
431+
object = getValue(object, type);
434432
}
435-
436-
return cb.equal(from.get(filter.getField()), object);
437-
}
433+
434+
if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
435+
return cb.equal(from.get(filter.getField()), object);
436+
}
437+
else if (filter.getCriterias().contains(PredicateFilter.Criteria.IN)
438+
|| filter.getCriterias().contains(PredicateFilter.Criteria.NIN)
439+
) {
440+
CriteriaBuilder.In<Object> in = cb.in(from.get(filter.getField()));
441+
442+
if(List.class.isInstance(object)) {
443+
List.class.cast(object)
444+
.forEach(in::value);
445+
} else {
446+
in.value(object);
447+
}
448+
449+
if(filter.getCriterias().contains(PredicateFilter.Criteria.NIN)) {
450+
return cb.not(in);
451+
}
452+
453+
return in;
454+
}
455+
}
438456
}
439457

440458
throw new IllegalArgumentException("Unsupported field type " + type + " for field " + predicateFilter.getField());
441459
}
442460

461+
private Object getValue(Object object, Class<?> type) {
462+
try {
463+
Constructor<?> constructor = type.getConstructor(Object.class);
464+
if(constructor != null) {
465+
Object arg = NullValue.class.isInstance(object) ? null : object;
466+
return constructor.newInstance(arg);
467+
}
468+
} catch (Exception e) {
469+
e.printStackTrace();
470+
}
471+
472+
return object;
473+
}
474+
475+
private Object getValues(Object object, Class<?> type) {
476+
List<Object> objects = new ArrayList<>();
477+
478+
for (Object value : List.class.cast(object).toArray()) {
479+
objects.add(getValue(value, type));
480+
}
481+
482+
return objects;
483+
}
484+
443485
/**
444486
* Makes predicate for field of primitive type
445487
*

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,137 @@ public void queryProcessVariablesWhereWithEQDoubleSearchCriteria() {
449449
}
450450

451451

452+
@Test
453+
public void queryProcessVariablesWhereWithINSingleValueSearchCriteria() {
454+
//given
455+
String query = "query {" +
456+
" TaskVariables(where: {"
457+
+ "value: {IN: 1.2345}"
458+
+ "}) {" +
459+
" select {" +
460+
" name" +
461+
" value" +
462+
" }" +
463+
" }" +
464+
"}";
465+
466+
String expected = "{TaskVariables={select=[{name=variable5, value=1.2345}]}}";
467+
468+
//when
469+
Object result = executor.execute(query).getData();
470+
471+
// then
472+
assertThat(result.toString()).isEqualTo(expected);
473+
}
474+
475+
@Test
476+
public void queryProcessVariablesWhereWithINListValueSearchCriteria() {
477+
//given
478+
String query = "query {" +
479+
" TaskVariables(where: {"
480+
+ "value: {IN: [1.2345]}"
481+
+ "}) {" +
482+
" select {" +
483+
" name" +
484+
" value" +
485+
" }" +
486+
" }" +
487+
"}";
488+
489+
String expected = "{TaskVariables={select=[{name=variable5, value=1.2345}]}}";
490+
491+
//when
492+
Object result = executor.execute(query).getData();
493+
494+
// then
495+
assertThat(result.toString()).isEqualTo(expected);
496+
}
497+
498+
@Test
499+
public void queryProcessVariablesWhereWithNINListValueSearchCriteria() {
500+
//given
501+
String query = "query {" +
502+
" TaskVariables(where: {"
503+
+ "value: {NIN: [null]}"
504+
+ "}) {" +
505+
" select {" +
506+
" name" +
507+
" value" +
508+
" }" +
509+
" }" +
510+
"}";
511+
512+
String expected = "{TaskVariables={select=["
513+
+ "{name=variable1, value=data}, "
514+
+ "{name=variable2, value=true}, "
515+
+ "{name=variable4, value={key=data}}, "
516+
+ "{name=variable5, value=1.2345}, "
517+
+ "{name=variable6, value=12345}, "
518+
+ "{name=variable7, value=[1, 2, 3, 4, 5]}]}}";
519+
520+
//when
521+
Object result = executor.execute(query).getData();
522+
523+
// then
524+
assertThat(result.toString()).isEqualTo(expected);
525+
}
526+
527+
@Test
528+
public void queryProcessVariablesWhereWithNINSingleValueSearchCriteria() {
529+
//given
530+
String query = "query {" +
531+
" TaskVariables(where: {"
532+
+ "value: {NIN: null}"
533+
+ "}) {" +
534+
" select {" +
535+
" name" +
536+
" value" +
537+
" }" +
538+
" }" +
539+
"}";
540+
541+
String expected = "{TaskVariables={select=["
542+
+ "{name=variable1, value=data}, "
543+
+ "{name=variable2, value=true}, "
544+
+ "{name=variable4, value={key=data}}, "
545+
+ "{name=variable5, value=1.2345}, "
546+
+ "{name=variable6, value=12345}, "
547+
+ "{name=variable7, value=[1, 2, 3, 4, 5]}]}}";
548+
549+
//when
550+
Object result = executor.execute(query).getData();
551+
552+
// then
553+
assertThat(result.toString()).isEqualTo(expected);
554+
}
555+
556+
@Test
557+
public void queryProcessVariablesWhereWithINListTypedValueSearchCriteria() {
558+
//given
559+
String query = "query {" +
560+
" TaskVariables(where: {"
561+
+ "value: {IN: [null, true, \"data\", 12345, 1.2345]}"
562+
+ "}) {" +
563+
" select {" +
564+
" name" +
565+
" value" +
566+
" }" +
567+
" }" +
568+
"}";
569+
570+
String expected = "{TaskVariables={select=["
571+
+ "{name=variable1, value=data}, "
572+
+ "{name=variable2, value=true}, "
573+
+ "{name=variable3, value=null}, "
574+
+ "{name=variable5, value=1.2345}, "
575+
+ "{name=variable6, value=12345}"
576+
+ "]}}";
577+
578+
//when
579+
Object result = executor.execute(query).getData();
580+
581+
// then
582+
assertThat(result.toString()).isEqualTo(expected);
583+
}
584+
452585
}

0 commit comments

Comments
 (0)