Skip to content

Commit a18a6ce

Browse files
authored
feat: add equals criteria support for custom attributes types (#133)
* feat: add equals criteria support for custom attributes converters * fix: failing tests * fix: clean up code and improve tests * fix: polish tests
1 parent 1a27eee commit a18a6ce

File tree

3 files changed

+171
-15
lines changed

3 files changed

+171
-15
lines changed

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.introproventures.graphql.jpa.query.schema.impl;
1818

19+
import java.lang.reflect.Constructor;
1920
import java.math.BigDecimal;
2021
import java.math.BigInteger;
2122
import java.util.Collection;
@@ -31,6 +32,7 @@
3132
import javax.persistence.criteria.Predicate;
3233

3334
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
35+
import graphql.language.NullValue;
3436

3537
/**
3638
* Supported types to build predicates for
@@ -413,9 +415,26 @@ else if(Collection.class.isAssignableFrom(type)) {
413415
return from.join(filter.getField()).in(value);
414416
} else if(type.isEnum()) {
415417
return getEnumPredicate((Path<Enum<?>>) field, predicateFilter);
416-
}
417-
else if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
418-
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
418+
} // TODO need better detection mechanism
419+
else if (Object.class.isAssignableFrom(type)) {
420+
if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
421+
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
422+
}
423+
else if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
424+
Object object = value;
425+
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();
434+
}
435+
436+
return cb.equal(from.get(filter.getField()), object);
437+
}
419438
}
420439

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

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

+145-9
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@
3232
import com.fasterxml.jackson.databind.JsonNode;
3333
import com.fasterxml.jackson.databind.ObjectMapper;
3434
import com.introproventures.graphql.jpa.query.converter.model.JsonEntity;
35+
import com.introproventures.graphql.jpa.query.converter.model.TaskVariableEntity;
3536
import com.introproventures.graphql.jpa.query.converter.model.VariableValue;
3637
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
3738
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
38-
import com.introproventures.graphql.jpa.query.schema.JavaScalars;
39-
import com.introproventures.graphql.jpa.query.schema.JavaScalars.GraphQLObjectCoercing;
4039
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
4140
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
42-
import graphql.schema.GraphQLScalarType;
4341
import org.junit.Test;
4442
import org.junit.runner.RunWith;
4543
import org.springframework.beans.factory.annotation.Autowired;
@@ -66,13 +64,9 @@ public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaB
6664

6765
@Bean
6866
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {
69-
70-
JavaScalars.register(JsonNode.class, new GraphQLScalarType("Json", "Json type", new GraphQLObjectCoercing()));
71-
JavaScalars.register(VariableValue.class, new GraphQLScalarType("VariableValue", "VariableValue Type", new GraphQLObjectCoercing()));
72-
7367
return new GraphQLJpaSchemaBuilder(entityManager)
74-
.name("HashMapSchema")
75-
.description("Json Entity test schema");
68+
.name("CustomAttributeConverterSchema")
69+
.description("Custom Attribute Converter Schema");
7670
}
7771

7872
}
@@ -121,6 +115,27 @@ public void criteriaTester() {
121115
assertThat(result).isNotEmpty();
122116
assertThat(result).hasSize(1);
123117
}
118+
119+
@Test
120+
@Transactional
121+
public void criteriaTester2() {
122+
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
123+
CriteriaQuery<TaskVariableEntity> criteria = builder.createQuery(TaskVariableEntity.class);
124+
Root<TaskVariableEntity> taskVariable = criteria.from(TaskVariableEntity.class);
125+
126+
Boolean object = new Boolean(true);
127+
128+
VariableValue<Boolean> variableValue = new VariableValue<>(object);
129+
criteria.select(taskVariable)
130+
.where(builder.equal(taskVariable.get("value"), variableValue));
131+
132+
// when:
133+
List<?> result = entityManager.createQuery(criteria).getResultList();
134+
135+
// then:
136+
assertThat(result).isNotEmpty();
137+
assertThat(result).hasSize(1);
138+
}
124139

125140
@Test // Problem with generating cast() in the where expression
126141
@Transactional
@@ -312,5 +327,126 @@ public void queryProcessVariablesWhereSearchCriteria() {
312327
// then
313328
assertThat(result.toString()).isEqualTo(expected);
314329
}
330+
331+
@Test
332+
public void queryProcessVariablesWhereWithEQStringSearchCriteria() {
333+
//given
334+
String query = "query {" +
335+
" TaskVariables(where: {"
336+
+ "name: {EQ: \"variable1\"}"
337+
+ "value: {EQ: \"data\"}"
338+
+ "}) {" +
339+
" select {" +
340+
" name" +
341+
" value" +
342+
" }" +
343+
" }" +
344+
"}";
345+
346+
String expected = "{TaskVariables={select=[{name=variable1, value=data}]}}";
347+
348+
//when
349+
Object result = executor.execute(query).getData();
350+
351+
// then
352+
assertThat(result.toString()).isEqualTo(expected);
353+
}
354+
355+
@Test
356+
public void queryProcessVariablesWhereWithEQBooleanSearchCriteria() {
357+
//given
358+
String query = "query {" +
359+
" TaskVariables(where: {"
360+
+ "name: {EQ: \"variable2\"}"
361+
+ "value: {EQ: true}"
362+
+ "}) {" +
363+
" select {" +
364+
" name" +
365+
" value" +
366+
" }" +
367+
" }" +
368+
"}";
369+
370+
String expected = "{TaskVariables={select=[{name=variable2, value=true}]}}";
371+
372+
//when
373+
Object result = executor.execute(query).getData();
374+
375+
// then
376+
assertThat(result.toString()).isEqualTo(expected);
377+
}
378+
379+
@Test
380+
public void queryProcessVariablesWhereWithEQNullSearchCriteria() {
381+
//given
382+
String query = "query {" +
383+
" TaskVariables(where: {"
384+
+ "name: {EQ: \"variable3\"}"
385+
+ "value: {EQ: null}"
386+
+ "}) {" +
387+
" select {" +
388+
" name" +
389+
" value" +
390+
" }" +
391+
" }" +
392+
"}";
393+
394+
String expected = "{TaskVariables={select=[{name=variable3, value=null}]}}";
395+
396+
//when
397+
Object result = executor.execute(query).getData();
398+
399+
// then
400+
assertThat(result.toString()).isEqualTo(expected);
401+
}
402+
403+
@Test
404+
public void queryProcessVariablesWhereWithEQIntSearchCriteria() {
405+
//given
406+
String query = "query {" +
407+
" TaskVariables(where: {"
408+
+ "name: {EQ: \"variable6\"}"
409+
+ "value: {EQ: 12345}"
410+
+ "}) {" +
411+
" select {" +
412+
" name" +
413+
" value" +
414+
" }" +
415+
" }" +
416+
"}";
417+
418+
String expected = "{TaskVariables={select=[{name=variable6, value=12345}]}}";
419+
420+
//when
421+
Object result = executor.execute(query).getData();
422+
423+
// then
424+
assertThat(result.toString()).isEqualTo(expected);
425+
}
426+
427+
@Test
428+
public void queryProcessVariablesWhereWithEQDoubleSearchCriteria() {
429+
//given
430+
String query = "query {" +
431+
" TaskVariables(where: {"
432+
+ "name: {EQ: \"variable5\"}"
433+
+ "value: {EQ: 1.2345}"
434+
+ "}) {" +
435+
" select {" +
436+
" name" +
437+
" value" +
438+
" }" +
439+
" }" +
440+
"}";
441+
442+
String expected = "{TaskVariables={select=[{name=variable5, value=1.2345}]}}";
443+
444+
//when
445+
Object result = executor.execute(query).getData();
446+
447+
// then
448+
assertThat(result.toString()).isEqualTo(expected);
449+
}
450+
315451

316452
}

graphql-jpa-query-schema/src/test/resources/GraphQLJpaConverterTests.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ insert into PROCESS_VARIABLE (create_time, execution_id, last_updated_time, name
99
insert into TASK_VARIABLE (create_time, execution_id, last_updated_time, name, process_instance_id, task_id, type, value) values
1010
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable1', 0, '1', 'string', '{"value":"data"}'),
1111
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable2', 0, '1', 'boolean', '{"value":true}'),
12-
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable3', 0, '2', 'string', '{"value":null}'),
12+
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable3', 0, '2', 'null', '{"value":null}'),
1313
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable4', 0, '2', 'json', '{"value":{"key":"data"}}'),
14-
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable5', 1, '4', 'double', '{"value":1.0}'),
15-
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable6', 1, '4', 'json', '{"value":[1,2,3,4,5]}');
14+
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable5', 1, '4', 'double', '{"value":1.2345}'),
15+
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable6', 1, '4', 'int', '{"value":12345}'),
16+
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}');

0 commit comments

Comments
 (0)