Skip to content

feat: add LOCATE predicate for JPA entity attributes annotated with @Convert #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ target/
.project
.springBeans
.classpath

.externalToolBuilders

6 changes: 6 additions & 0 deletions graphql-jpa-query-schema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public Object get(DataFetchingEnvironment environment) {

//EntityGraph<?> entityGraph = buildEntityGraph(new Field("select", new SelectionSet(Arrays.asList(field))));

// Let's clear session persistent context to avoid getting stale objects cached in the same session
// between requests with different search criteria. This looks like a Hibernate bug...
entityManager.clear();

return getQuery(environment, field, true)
//.setHint("javax.persistence.fetchgraph", entityGraph) // TODO: fix runtime exception
.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.persistence.Convert;
import javax.persistence.EntityManager;
import javax.persistence.Transient;
import javax.persistence.metamodel.Attribute;
Expand All @@ -43,9 +44,6 @@
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
Expand All @@ -55,7 +53,6 @@
import com.introproventures.graphql.jpa.query.schema.NamingStrategy;
import com.introproventures.graphql.jpa.query.schema.impl.IntrospectionUtils.CachedIntrospectionResult.CachedPropertyDescriptor;
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;

import graphql.Assert;
import graphql.Scalars;
import graphql.schema.Coercing;
Expand All @@ -73,6 +70,8 @@
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLTypeReference;
import graphql.schema.PropertyDataFetcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* JPA specific schema builder implementation of {code #GraphQLSchemaBuilder} interface
Expand Down Expand Up @@ -437,6 +436,16 @@ private GraphQLInputType getWhereAttributeType(Attribute<?,?> attribute) {
.type(getAttributeInputType(attribute))
.build()
);
}
else if (attribute.getJavaMember().getClass().isAssignableFrom(Field.class)
&& Field.class.cast(attribute.getJavaMember())
.isAnnotationPresent(Convert.class))
{
builder.field(GraphQLInputObjectField.newInputObjectField()
.name(Criteria.LOCATE.name())
.description("Locate search criteria")
.type(getAttributeInputType(attribute))
.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ else if(Collection.class.isAssignableFrom(type)) {
} else if(type.isEnum()) {
return getEnumPredicate((Path<Enum<?>>) field, predicateFilter);
}
else if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
}

throw new IllegalArgumentException("Unsupported field type " + type + " for field " + predicateFilter.getField());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public enum Criteria {
/**
* Not Between condition
*/
NOT_BETWEEN;
NOT_BETWEEN,

/**
* JPA's LOCATE predicate for attributes annotated with @Convert
*/
LOCATE;

private static Set<String> names = EnumSet.allOf(Criteria.class)
.stream()
Expand Down
Loading