Skip to content

Commit 0d969c1

Browse files
authored
Implemented support for parameter bindings in reverse queries (#17)
* Support parameter bindings in reverse queries * Update README.md
1 parent 36f582d commit 0d969c1

File tree

11 files changed

+132
-47
lines changed

11 files changed

+132
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#.project
44
.settings/
55
build/
6+
.factorypath
67

78
# Intellij
89
.idea/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ For GraphQL JPA Annotations use:
3434
For GraphQL JPA Schema Builder use:
3535

3636
<dependency>
37-
<groupId>com.github.introproventures.graphql-jpa-query</groupId>
37+
<groupId>com.introproventures</groupId>
3838
<artifactId>graphql-jpa-query-schema</artifactId>
3939
<version>0.3.0</version>
4040
</dependency>
@@ -215,7 +215,7 @@ Will Return:
215215

216216
Reverse Query
217217
-------------
218-
You can execute an inverse query to fitler results with a join in many-to-one association with some limitations. If you do this, be aware that only static parameter binding are supported in `where` criteria expressions.
218+
You can execute an inverse query to fitler results with a join in many-to-one association in one query with parameter bindings support added in 0.3.1
219219

220220
For Example:
221221

graphql-jpa-query-annotations/.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
2333
</buildSpec>
2434
<natures>
35+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
2536
<nature>org.eclipse.m2e.core.maven2Nature</nature>
2637
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
2738
<nature>org.eclipse.jdt.core.javanature</nature>
Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" output="target/classes" path="src/main/java">
4-
<attributes>
5-
<attribute name="optional" value="true"/>
6-
<attribute name="maven.pomderived" value="true"/>
7-
</attributes>
8-
</classpathentry>
9-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10-
<attributes>
11-
<attribute name="maven.pomderived" value="true"/>
12-
</attributes>
13-
</classpathentry>
14-
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
15-
<attributes>
16-
<attribute name="maven.pomderived" value="true"/>
17-
</attributes>
18-
</classpathentry>
19-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
20-
<attributes>
21-
<attribute name="optional" value="true"/>
22-
<attribute name="maven.pomderived" value="true"/>
23-
</attributes>
24-
</classpathentry>
25-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
26-
<attributes>
27-
<attribute name="maven.pomderived" value="true"/>
28-
</attributes>
29-
</classpathentry>
30-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31-
<attributes>
32-
<attribute name="maven.pomderived" value="true"/>
33-
</attributes>
34-
</classpathentry>
35-
<classpathentry kind="con" path="org.eclipse.ajdt.core.ASPECTJRT_CONTAINER"/>
36-
<classpathentry kind="src" path="target/generated-sources/annotations">
37-
<attributes>
38-
<attribute name="optional" value="true"/>
39-
</attributes>
40-
</classpathentry>
41-
<classpathentry kind="output" path="target/classes"/>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
15+
<attributes>
16+
<attribute name="maven.pomderived" value="true"/>
17+
</attributes>
18+
</classpathentry>
19+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
20+
<attributes>
21+
<attribute name="optional" value="true"/>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="con" path="org.eclipse.ajdt.core.ASPECTJRT_CONTAINER"/>
36+
<classpathentry kind="src" path="target/generated-sources/annotations">
37+
<attributes>
38+
<attribute name="optional" value="true"/>
39+
</attributes>
40+
</classpathentry>
41+
<classpathentry kind="src" path=".apt_generated">
42+
<attributes>
43+
<attribute name="optional" value="true"/>
44+
</attributes>
45+
</classpathentry>
46+
<classpathentry kind="output" path="target/classes"/>
4247
</classpath>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
2333
</buildSpec>
2434
<natures>
35+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
2536
<nature>org.eclipse.m2e.core.maven2Nature</nature>
2637
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
2738
<nature>org.eclipse.jdt.core.javanature</nature>

graphql-jpa-query-example/.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
2333
</buildSpec>
2434
<natures>
35+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
2536
<nature>org.eclipse.m2e.core.maven2Nature</nature>
2637
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
2738
<nature>org.eclipse.jdt.core.javanature</nature>

graphql-jpa-query-schema/.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
2333
</buildSpec>
2434
<natures>
35+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
2536
<nature>org.eclipse.m2e.core.maven2Nature</nature>
2637
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
2738
<nature>org.eclipse.jdt.core.javanature</nature>

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

Lines changed: 13 additions & 1 deletion
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.util.Collections;
1920
import java.util.Map;
2021

2122
import javax.transaction.Transactional;
@@ -62,10 +63,21 @@ public ExecutionResult execute(String query) {
6263
@Override
6364
@Transactional(TxType.SUPPORTS)
6465
public ExecutionResult execute(String query, Map<String, Object> arguments) {
66+
67+
// Need to inject variables in context to support parameter bindings in reverse queries
68+
Map<String, Object> context = Collections.singletonMap("variables", arguments);
69+
70+
// ExecutionInput executionInput = ExecutionInput.newExecutionInput()
71+
// .query(query)
72+
// .variables(arguments)
73+
// .root(context)
74+
// .context(context)
75+
// .build();
76+
6577
if (arguments == null)
6678
return graphQL.execute(query);
6779
else
68-
return graphQL.execute(query, (Object) null, arguments);
80+
return graphQL.execute(query, context, arguments);
6981
}
7082

7183
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ protected Predicate getPredicate(CriteriaBuilder cb, Root<?> from, From<?,?> pat
282282

283283
From<?,?> join = getCompoundJoin(path, argument.getName(), false);
284284
Argument where = new Argument("where", argument.getValue());
285+
Map<String, Object> variables = Optional.ofNullable(environment.getContext())
286+
.filter(it -> it instanceof Map)
287+
.map(it -> (Map<String, Object>) it)
288+
.map(it -> (Map<String, Object>) it.get("variables"))
289+
.orElse(Collections.emptyMap());
285290

286291
GraphQLFieldDefinition fieldDef = getFieldDef(
287292
environment.getGraphQLSchema(),
@@ -290,7 +295,7 @@ protected Predicate getPredicate(CriteriaBuilder cb, Root<?> from, From<?,?> pat
290295
);
291296

292297
Map<String, Object> arguments = (Map<String, Object>) new ValuesResolver()
293-
.getArgumentValues(fieldDef.getArguments(), Collections.singletonList(where), Collections.emptyMap())
298+
.getArgumentValues(fieldDef.getArguments(), Collections.singletonList(where), variables)
294299
.get("where");
295300

296301
return getWherePredicate(cb, from, join, new WherePredicateEnvironment(environment, arguments), where);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class JavaScalarsTest {
3434
@Test
3535
public void long2LocalDateTime() {
3636
//given
37-
Coercing coercing = JavaScalars.of(LocalDateTime.class).getCoercing();
37+
Coercing<?,?> coercing = JavaScalars.of(LocalDateTime.class).getCoercing();
3838

3939
LocalDateTime localDateTime = LocalDateTime.of(2017, 02, 02, 12, 30, 15);
4040
long input = localDateTime.toEpochSecond(ZoneId.systemDefault().getRules().getOffset(localDateTime));
@@ -58,7 +58,7 @@ public void long2LocalDateTime() {
5858
@Test
5959
public void string2LocalDateTime() {
6060
//given
61-
Coercing coercing = JavaScalars.of(LocalDateTime.class).getCoercing();
61+
Coercing<?,?> coercing = JavaScalars.of(LocalDateTime.class).getCoercing();
6262
final String input = "2017-02-02T12:30:15";
6363

6464
//when
@@ -79,7 +79,7 @@ public void string2LocalDateTime() {
7979

8080
public void long2LocalDate() {
8181
// given
82-
Coercing coercing = JavaScalars.of(LocalDate.class).getCoercing();
82+
Coercing<?,?> coercing = JavaScalars.of(LocalDate.class).getCoercing();
8383
LocalDateTime localDateTime = LocalDateTime.of(2017, 02, 02, 0, 0, 0);
8484
long input = localDateTime.toEpochSecond(ZoneId.systemDefault().getRules().getOffset(localDateTime));
8585

@@ -98,7 +98,7 @@ public void long2LocalDate() {
9898

9999
public void string2LocalDate() {
100100
//given
101-
Coercing coercing = JavaScalars.of(LocalDate.class).getCoercing();
101+
Coercing<?,?> coercing = JavaScalars.of(LocalDate.class).getCoercing();
102102
final String input = "2017-02-02";
103103

104104
//when

0 commit comments

Comments
 (0)