Skip to content

Commit bde04d5

Browse files
miklemvigdianov
authored andcommitted
distinct sql and ignore field in filters and sorting (#95)
* change distinct and annotation filter and sort * fix - distinct params to scheme builder * fix sub filter
1 parent 4c8bf31 commit bde04d5

File tree

15 files changed

+379
-20
lines changed

15 files changed

+379
-20
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.introproventures.graphql.jpa.query.annotation;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import static java.lang.annotation.ElementType.FIELD;
7+
import static java.lang.annotation.ElementType.TYPE;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
@Target( { TYPE, FIELD })
11+
@Retention(RUNTIME)
12+
public @interface GraphQLIgnoreFilter {
13+
}
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.introproventures.graphql.jpa.query.annotation;
2+
3+
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.FIELD;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
@Target( { FIELD })
11+
@Retention(RUNTIME)
12+
public @interface GraphQLIgnoreOrder {
13+
}
14+

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLJpaQueryProperties.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public class GraphQLJpaQueryProperties {
3636
@NotEmpty
3737
private String description;
3838

39+
/**
40+
* Enable or disable distinct parameter.
41+
*/
42+
private boolean useDistinctParameter;
43+
44+
/**
45+
* Enable or disable distinct distinct sql query fetcher.
46+
*/
47+
private boolean distinctFetcher;
48+
3949
/**
4050
* Enable or disable QraphQL module services.
4151
*/
@@ -78,6 +88,34 @@ public void setDescription(String description) {
7888
this.description = description;
7989
}
8090

91+
/**
92+
* @return the useDistinctParameter
93+
*/
94+
public boolean isUseDistinctParameter() {
95+
return useDistinctParameter;
96+
}
97+
98+
/**
99+
* @param useDistinctParameter the useDistinctParameter to set
100+
*/
101+
public void setUseDistinctParameter(boolean useDistinctParameter) {
102+
this.useDistinctParameter = useDistinctParameter;
103+
}
104+
105+
/**
106+
* @return the distinctFetcher
107+
*/
108+
public boolean isDistinctFetcher() {
109+
return distinctFetcher;
110+
}
111+
112+
/**
113+
* @param distinctFetcher the distinctFetcher to set
114+
*/
115+
public void setDistinctFetcher(boolean distinctFetcher) {
116+
this.distinctFetcher = distinctFetcher;
117+
}
118+
81119
/**
82120
* @return the enabled
83121
*/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
spring.graphql.jpa.query.name=Query
22
spring.graphql.jpa.query.description=
3+
spring.graphql.jpa.query.useDistinctParameter=false
4+
spring.graphql.jpa.query.distinctFetcher=false
35
spring.graphql.jpa.query.enabled=true
46
spring.graphql.jpa.query.path=/graphql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
spring.graphql.jpa.query.name=GraphQLJpaQuery
22
spring.graphql.jpa.query.description=GraphQL Jpa Query Schema Specification
3+
spring.graphql.jpa.query.useDistinctParameter=false
4+
spring.graphql.jpa.query.distinctFetcher=false
35
spring.graphql.jpa.query.enabled=true
46
spring.graphql.jpa.query.path=/graphql

graphql-jpa-query-example-merge/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<groupId>com.introproventures</groupId>
3333
<artifactId>graphql-jpa-query-autoconfigure</artifactId>
3434
</dependency>
35-
35+
3636
<dependency>
3737
<groupId>org.springframework.boot</groupId>
3838
<artifactId>spring-boot-starter-web</artifactId>

graphql-jpa-query-example-merge/src/main/java/com/introproventures/graphql/jpa/query/example/books/Book.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import javax.persistence.Id;
2424
import javax.persistence.ManyToOne;
2525

26+
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
27+
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreOrder;
2628
import lombok.Data;
2729

2830
@Data
@@ -31,6 +33,8 @@ public class Book {
3133
@Id
3234
Long id;
3335

36+
@GraphQLIgnoreOrder
37+
@GraphQLIgnoreFilter
3438
String title;
3539

3640
@ManyToOne(fetch=FetchType.LAZY)

graphql-jpa-query-example-merge/src/main/java/com/introproventures/graphql/jpa/query/example/books/BooksSchemaConfiguration.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import javax.persistence.EntityManagerFactory;
88
import javax.sql.DataSource;
99

10+
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLJpaQueryProperties;
1011
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
1112
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
1213
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
1314
import org.hibernate.cfg.AvailableSettings;
1415
import org.hibernate.dialect.H2Dialect;
16+
import org.springframework.beans.factory.annotation.Autowired;
1517
import org.springframework.beans.factory.annotation.Qualifier;
1618
import org.springframework.boot.context.properties.ConfigurationProperties;
1719
import org.springframework.boot.jdbc.DataSourceBuilder;
@@ -74,14 +76,22 @@ public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConf
7476

7577
private final EntityManager entityManager;
7678

79+
@Autowired
80+
private GraphQLJpaQueryProperties properties;
81+
7782
public GraphQLJpaQuerySchemaConfigurer(@Qualifier("bookEntityManager") EntityManagerFactory entityManager) {
7883
this.entityManager = entityManager.createEntityManager();
7984
}
8085

8186
@Override
8287
public void configure(GraphQLShemaRegistration registry) {
83-
84-
registry.register(new GraphQLJpaSchemaBuilder(entityManager).name("GraphQLBooks").build());
88+
registry.register(
89+
new GraphQLJpaSchemaBuilder(entityManager)
90+
.name("GraphQLBooks")
91+
.useDistinctParameter(properties.isUseDistinctParameter())
92+
.setDistinctFetcher(properties.isDistinctFetcher())
93+
.build()
94+
);
8595
}
8696
}
8797

graphql-jpa-query-example-merge/src/main/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ spring:
1010
query:
1111
name: Query
1212
description: Combined GraphQL Jpa Query for Starwars and Books Example
13+
useDistinctParameter: true
1314
enabled: true
15+
path: graphql
1416

1517
starwars:
1618
hikari:

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
*
4747
*/
4848
class GraphQLJpaQueryDataFetcher extends QraphQLJpaBaseDataFetcher {
49+
50+
private boolean defaultDistinct = false;
4951

5052
private static final String HIBERNATE_QUERY_PASS_DISTINCT_THROUGH = "hibernate.query.passDistinctThrough";
5153
private static final String ORG_HIBERNATE_CACHEABLE = "org.hibernate.cacheable";
@@ -57,6 +59,19 @@ public GraphQLJpaQueryDataFetcher(EntityManager entityManager, EntityType<?> ent
5759
super(entityManager, entityType);
5860
}
5961

62+
public GraphQLJpaQueryDataFetcher(EntityManager entityManager, EntityType<?> entityType, boolean defaultDistinct) {
63+
super(entityManager, entityType);
64+
this.defaultDistinct = defaultDistinct;
65+
}
66+
67+
public boolean isDefaultDistinct() {
68+
return defaultDistinct;
69+
}
70+
71+
public void setDefaultDistinct(boolean defaultDistinct) {
72+
this.defaultDistinct = defaultDistinct;
73+
}
74+
6075
@Override
6176
public Object get(DataFetchingEnvironment environment) {
6277
Field field = environment.getFields().iterator().next();
@@ -69,7 +84,7 @@ public Object get(DataFetchingEnvironment environment) {
6984

7085
Optional<Argument> pageArgument = getPageArgument(field);
7186
Page page = extractPageArgument(environment, field);
72-
Argument distinctArg = extractArgument(environment, field, GraphQLJpaSchemaBuilder.SELECT_DISTINCT_PARAM_NAME, new BooleanValue(true));
87+
Argument distinctArg = extractArgument(environment, field, GraphQLJpaSchemaBuilder.SELECT_DISTINCT_PARAM_NAME, new BooleanValue(defaultDistinct));
7388

7489
boolean isDistinct = ((BooleanValue) distinctArg.getValue()).isValue();
7590

0 commit comments

Comments
 (0)