Skip to content

Commit 388851a

Browse files
anotenderigdianov
authored andcommitted
Add GraphQLWhereVariableBindingsTests and fix variable name mismatch issue (#164)
1 parent c261ec9 commit 388851a

File tree

2 files changed

+303
-7
lines changed

2 files changed

+303
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ private <R extends Value<?>> R getValue(Argument argument, DataFetchingEnvironme
395395

396396
GraphQLArgument graphQLArgument = environment.getExecutionStepInfo()
397397
.getFieldDefinition()
398-
.getArgument(variableName);
398+
.getArgument(argument.getName());
399399

400400
return (R) AstValueHelper.astFromValue(variableValue, graphQLArgument.getType());
401401
}
@@ -918,19 +918,19 @@ else if (value instanceof VariableReference) {
918918
}
919919
} else if (value instanceof ArrayValue) {
920920
Object convertedValue = environment.getArgument(argument.getName());
921-
921+
922922
if (convertedValue != null && getJavaType(environment, argument).isEnum()) {
923-
923+
924924
Function<Object, Value> f = (obj) -> Value.class.isInstance(obj)
925-
? Value.class.cast(obj)
925+
? Value.class.cast(obj)
926926
: new EnumValue(obj.toString());
927-
927+
928928
// unwrap [[EnumValue{name='value'}]]
929929
if(convertedValue instanceof Collection
930930
&& ((Collection) convertedValue).stream().allMatch(it->it instanceof Collection)) {
931931
convertedValue = ((Collection) convertedValue).iterator().next();
932932
}
933-
933+
934934
if(convertedValue instanceof Collection) {
935935
return ((Collection) convertedValue).stream()
936936
.map((it) -> convertValue(environment, argument, f.apply(it)))
@@ -939,7 +939,7 @@ else if (value instanceof VariableReference) {
939939
// Return real typed resolved array value
940940
return convertValue(environment, argument, f.apply(convertedValue));
941941
}
942-
else
942+
else
943943
if (convertedValue != null && !getJavaType(environment, argument).isEnum()) {
944944
// unwrap [[EnumValue{name='value'}]]
945945
if(convertedValue instanceof Collection
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
package com.introproventures.graphql.jpa.query.schema;
2+
3+
import static org.assertj.core.api.Assertions.tuple;
4+
import static org.assertj.core.api.BDDAssertions.then;
5+
6+
import static com.introproventures.graphql.jpa.query.schema.model.book.Genre.NOVEL;
7+
import static com.introproventures.graphql.jpa.query.schema.model.book.Genre.PLAY;
8+
9+
import java.io.IOException;
10+
import java.util.Map;
11+
12+
import javax.persistence.EntityManager;
13+
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.autoconfigure.SpringBootApplication;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.test.context.TestPropertySource;
22+
import org.springframework.test.context.junit4.SpringRunner;
23+
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
26+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
27+
28+
import graphql.ExecutionResult;
29+
30+
@RunWith(SpringRunner.class)
31+
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
32+
@TestPropertySource({ "classpath:hibernate.properties" })
33+
public class GraphQLWhereVariableBindingsTests {
34+
35+
@SpringBootApplication
36+
static class Application {
37+
38+
@Bean
39+
public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaBuilder) {
40+
return new GraphQLJpaExecutor(graphQLSchemaBuilder.build());
41+
}
42+
43+
@Bean
44+
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {
45+
return new GraphQLJpaSchemaBuilder(entityManager)
46+
.name("GraphQLBooks")
47+
.description("Books JPA test schema");
48+
}
49+
50+
}
51+
52+
@Autowired
53+
private GraphQLExecutor executor;
54+
55+
@Test
56+
public void queryWithSimpleEqualsVariableBinding() throws IOException {
57+
//given
58+
String query = "" +
59+
"query($where: BooksCriteriaExpression) {" +
60+
" Books(where: $where) {" +
61+
" select {" +
62+
" id" +
63+
" title" +
64+
" genre" +
65+
" }" +
66+
" }" +
67+
"}";
68+
String variables = "" +
69+
"{" +
70+
" \"where\": {" +
71+
" \"title\": {" +
72+
" \"EQ\": \"War and Peace\"" +
73+
" }" +
74+
" }" +
75+
"}";
76+
77+
//when
78+
ExecutionResult executionResult = executor.execute(query, getVariablesMap(variables));
79+
80+
// then
81+
then(executionResult.getErrors()).isEmpty();
82+
Map<String, Object> result = executionResult.getData();
83+
then(result)
84+
.extracting("Books")
85+
.flatExtracting("select")
86+
.hasSize(1)
87+
.extracting("id", "title", "genre")
88+
.containsOnly(tuple(2L, "War and Peace", NOVEL));
89+
}
90+
91+
@Test
92+
public void queryWithNestedWhereClauseVariableBinding() throws IOException {
93+
//given
94+
String query = "" +
95+
"query($where: BooksCriteriaExpression) {" +
96+
" Books(where: $where) {" +
97+
" select {" +
98+
" author {" +
99+
" id" +
100+
" name" +
101+
" }" +
102+
" title" +
103+
" }" +
104+
" }" +
105+
"}";
106+
String variables = "" +
107+
"{" +
108+
" \"where\": {" +
109+
" \"author\": {" +
110+
" \"name\": {" +
111+
" \"EQ\": \"Leo Tolstoy\"" +
112+
" }" +
113+
" }" +
114+
" }" +
115+
"}";
116+
117+
//when
118+
ExecutionResult executionResult = executor.execute(query, getVariablesMap(variables));
119+
120+
// then
121+
then(executionResult.getErrors()).isEmpty();
122+
Map<String, Object> result = executionResult.getData();
123+
then(result)
124+
.extracting("Books")
125+
.flatExtracting("select")
126+
.hasSize(2)
127+
.extracting("title")
128+
.containsOnly("War and Peace", "Anna Karenina");
129+
then(result)
130+
.extracting("Books")
131+
.flatExtracting("select")
132+
.hasSize(2)
133+
.extracting("author")
134+
.extracting("id", "name")
135+
.containsOnly(tuple(1L, "Leo Tolstoy"));
136+
}
137+
138+
@Test
139+
public void queryWithInVariableBinding() throws IOException {
140+
//given
141+
String query = "" +
142+
"query($where: BooksCriteriaExpression) {" +
143+
" Books(where: $where) {" +
144+
" select {" +
145+
" id" +
146+
" title" +
147+
" genre" +
148+
" }" +
149+
" }" +
150+
"}";
151+
String variables = "" +
152+
"{" +
153+
" \"where\": {" +
154+
" \"genre\": {" +
155+
" \"IN\": [\"PLAY\"]" +
156+
" }" +
157+
" }" +
158+
"}";
159+
160+
//when
161+
ExecutionResult executionResult = executor.execute(query, getVariablesMap(variables));
162+
163+
// then
164+
then(executionResult.getErrors()).isEmpty();
165+
Map<String, Object> result = executionResult.getData();
166+
then(result)
167+
.extracting("Books")
168+
.flatExtracting("select")
169+
.extracting("genre")
170+
.containsOnly(PLAY);
171+
}
172+
173+
@Test
174+
public void queryWithMultipleRestrictionForOneProperty() throws IOException {
175+
//given
176+
String query = "" +
177+
"query($where: BooksCriteriaExpression) {" +
178+
" Books(where: $where) {" +
179+
" select {" +
180+
" id" +
181+
" title" +
182+
" }" +
183+
" }" +
184+
"}";
185+
String variables = "" +
186+
"{" +
187+
" \"where\": {" +
188+
" \"id\": {" +
189+
" \"GE\": 5," +
190+
" \"LE\": 7" +
191+
" }" +
192+
" }" +
193+
"}";
194+
195+
//when
196+
ExecutionResult executionResult = executor.execute(query, getVariablesMap(variables));
197+
198+
// then
199+
then(executionResult.getErrors()).isEmpty();
200+
Map<String, Object> result = executionResult.getData();
201+
then(result)
202+
.extracting("Books")
203+
.flatExtracting("select")
204+
.extracting("id", "title")
205+
.containsOnly(
206+
tuple(5L, "The Cherry Orchard"),
207+
tuple(6L, "The Seagull"),
208+
tuple(7L, "Three Sisters")
209+
);
210+
}
211+
212+
@Test
213+
public void queryWithPropertyWhereVariableBinding() throws IOException {
214+
//given
215+
String query = "" +
216+
"query($booksWhereClause: BooksCriteriaExpression) {" +
217+
" Authors {" +
218+
" select {" +
219+
" name" +
220+
" books(where: $booksWhereClause) {" +
221+
" genre" +
222+
" }" +
223+
" }" +
224+
" }" +
225+
"}";
226+
String variables = "" +
227+
"{" +
228+
" \"booksWhereClause\": {" +
229+
" \"genre\": {" +
230+
" \"IN\": [\"NOVEL\"]" +
231+
" }" +
232+
" }" +
233+
"}";
234+
235+
//when
236+
ExecutionResult executionResult = executor.execute(query, getVariablesMap(variables));
237+
238+
// then
239+
then(executionResult.getErrors()).isEmpty();
240+
Map<String, Object> result = executionResult.getData();
241+
then(result)
242+
.extracting("Authors")
243+
.flatExtracting("select")
244+
.extracting("name")
245+
.containsOnly("Leo Tolstoy");
246+
then(result)
247+
.extracting("Authors")
248+
.flatExtracting("select")
249+
.flatExtracting("books")
250+
.extracting("genre")
251+
.containsOnly(NOVEL);
252+
}
253+
254+
@Test
255+
public void queryWithRestrictionsForMultipleProperties() throws IOException {
256+
//given
257+
String query = "" +
258+
"query($where: BooksCriteriaExpression) {" +
259+
" Books(where: $where) {" +
260+
" select {" +
261+
" id" +
262+
" title" +
263+
" }" +
264+
" }" +
265+
"}";
266+
String variables = "" +
267+
"{" +
268+
" \"where\": {" +
269+
" \"title\": {" +
270+
" \"LIKE\": \"The\"" +
271+
" }," +
272+
" \"id\": {" +
273+
" \"LT\": 6" +
274+
" }" +
275+
" }" +
276+
"}";
277+
278+
//when
279+
ExecutionResult executionResult = executor.execute(query, getVariablesMap(variables));
280+
281+
// then
282+
then(executionResult.getErrors()).isEmpty();
283+
Map<String, Object> result = executionResult.getData();
284+
then(result)
285+
.extracting("Books")
286+
.flatExtracting("select")
287+
.extracting("id", "title")
288+
.containsOnly(tuple(5L, "The Cherry Orchard"));
289+
}
290+
291+
@SuppressWarnings("unchecked")
292+
private Map<String, Object> getVariablesMap(String variables) throws IOException {
293+
ObjectMapper mapper = new ObjectMapper();
294+
return (Map<String, Object>) mapper.readValue(variables, Map.class);
295+
}
296+
}

0 commit comments

Comments
 (0)