Skip to content

Commit ae25aba

Browse files
authored
fix: removed EntityManager.clear() from GraphQLJpaQueryDataFetcher (#192)
* fix: removed EntityManager.clear() from GraphQLJpaQueryDataFetcher * fix(test): added unit tests to try to reproduce stale caching result problem
1 parent 7ce947d commit ae25aba

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ public Object get(DataFetchingEnvironment environment) {
108108

109109
queryField = new Field(fieldName, field.getArguments(), recordsSelection.get().getSelectionSet());
110110

111-
// Let's clear session persistent context to avoid getting stale objects cached in the same session
112-
// between requests with different search criteria. This looks like a Hibernate bug...
113-
entityManager.clear();
114-
115111
TypedQuery<?> query = getQuery(queryEnvironment, queryField, isDistinct);
116112

117113
// Let's apply page only if present

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

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.assertj.core.util.Lists.list;
2222

2323
import java.util.Arrays;
24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -1471,6 +1472,100 @@ public void queryWithEQMatchingCase() {
14711472

14721473
//then:
14731474
assertThat(result.toString()).isEqualTo(expected);
1474-
}
1475+
}
1476+
1477+
@Test
1478+
public void shouldNotReturnStaleCacheResultsFromPreviousQueryForCollectionCriteriaExpression() {
1479+
//given:
1480+
String query = "query ($genre: Genre) {" +
1481+
" Authors(where: { " +
1482+
" books: {" +
1483+
" genre: {EQ: $genre}" +
1484+
" }" +
1485+
" }) {" +
1486+
" select {" +
1487+
" id" +
1488+
" name" +
1489+
" books {" +
1490+
" id" +
1491+
" title" +
1492+
" genre" +
1493+
" }" +
1494+
" }" +
1495+
" }" +
1496+
"}";
1497+
1498+
//when: 1st query
1499+
Object result1 = executor.execute(query, Collections.singletonMap("genre", "PLAY")).getData();
1500+
1501+
String expected1 = "{Authors={select=["
1502+
+ "{id=4, name=Anton Chekhov, books=["
1503+
+ "{id=5, title=The Cherry Orchard, genre=PLAY}, "
1504+
+ "{id=6, title=The Seagull, genre=PLAY}, "
1505+
+ "{id=7, title=Three Sisters, genre=PLAY}"
1506+
+ "]}"
1507+
+ "]}}";
1508+
1509+
//then:
1510+
assertThat(result1.toString()).isEqualTo(expected1);
1511+
1512+
//when: 2nd query
1513+
Object result2 = executor.execute(query, Collections.singletonMap("genre", "NOVEL")).getData();
1514+
1515+
String expected2 = "{Authors={select=["
1516+
+ "{id=1, name=Leo Tolstoy, books=["
1517+
+ "{id=2, title=War and Peace, genre=NOVEL}, "
1518+
+ "{id=3, title=Anna Karenina, genre=NOVEL}"
1519+
+ "]}"
1520+
+ "]}}";
1521+
1522+
//then:
1523+
assertThat(result2.toString()).isEqualTo(expected2);
1524+
}
1525+
1526+
@Test
1527+
public void shouldNotReturnStaleCacheResultsFromPreviousQueryForEmbeddedCriteriaExpression() {
1528+
//given:
1529+
String query = "query ($genre: Genre) {" +
1530+
" Authors {" +
1531+
" select {" +
1532+
" id" +
1533+
" name" +
1534+
" books(where:{ genre: {EQ: $genre} }) {" +
1535+
" id" +
1536+
" title" +
1537+
" genre" +
1538+
" }" +
1539+
" }" +
1540+
" }" +
1541+
"}";
1542+
1543+
//when: 1st query
1544+
Object result1 = executor.execute(query, Collections.singletonMap("genre", "PLAY")).getData();
1545+
1546+
String expected1 = "{Authors={select=["
1547+
+ "{id=4, name=Anton Chekhov, books=["
1548+
+ "{id=5, title=The Cherry Orchard, genre=PLAY}, "
1549+
+ "{id=6, title=The Seagull, genre=PLAY}, "
1550+
+ "{id=7, title=Three Sisters, genre=PLAY}"
1551+
+ "]}"
1552+
+ "]}}";
1553+
1554+
//then:
1555+
assertThat(result1.toString()).isEqualTo(expected1);
1556+
1557+
//when: 2nd query
1558+
Object result2 = executor.execute(query, Collections.singletonMap("genre", "NOVEL")).getData();
1559+
1560+
String expected2 = "{Authors={select=["
1561+
+ "{id=1, name=Leo Tolstoy, books=["
1562+
+ "{id=2, title=War and Peace, genre=NOVEL}, "
1563+
+ "{id=3, title=Anna Karenina, genre=NOVEL}"
1564+
+ "]}"
1565+
+ "]}}";
1566+
1567+
//then:
1568+
assertThat(result2.toString()).isEqualTo(expected2);
1569+
}
14751570

14761571
}

0 commit comments

Comments
 (0)