Skip to content

Commit 95f494a

Browse files
committed
Fix returning response from cache fix #289
1 parent 87118ec commit 95f494a

File tree

5 files changed

+81
-43
lines changed

5 files changed

+81
-43
lines changed

examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleServlet.java

-11
This file was deleted.

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/OsgiGraphQLHttpServlet.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
@Component(
5454
service = {javax.servlet.http.HttpServlet.class, javax.servlet.Servlet.class},
55-
property = {"alias=/graphql", "service.description=GraphQL HTTP Servlet"},
55+
property = {"service.description=GraphQL HTTP Servlet"},
5656
immediate = true
5757
)
5858
@Designate(ocd = OsgiGraphQLHttpServletConfiguration.class, factory = true)
@@ -132,12 +132,8 @@ protected void updateSchema() {
132132
updateFuture.cancel(true);
133133
}
134134

135-
updateFuture = executor.schedule(new Runnable() {
136-
@Override
137-
public void run() {
138-
doUpdateSchema();
139-
}
140-
}, schemaUpdateDelay, TimeUnit.MILLISECONDS);
135+
updateFuture = executor
136+
.schedule(this::doUpdateSchema, schemaUpdateDelay, TimeUnit.MILLISECONDS);
141137
}
142138
}
143139

@@ -148,17 +144,17 @@ private void doUpdateSchema() {
148144
if (!queryProviders.isEmpty()) {
149145
for (GraphQLQueryProvider provider : queryProviders) {
150146
if (provider.getQueries() != null && !provider.getQueries().isEmpty()) {
151-
provider.getQueries().forEach(queryTypeBuilder::field);
147+
provider.getQueries().forEach(queryTypeBuilder::field);
152148
}
153149
}
154150
} else {
155151
// graphql-java enforces Query type to be there with at least some field.
156152
queryTypeBuilder.field(
157-
GraphQLFieldDefinition
158-
.newFieldDefinition()
159-
.name("_empty")
160-
.type(Scalars.GraphQLBoolean)
161-
.build());
153+
GraphQLFieldDefinition
154+
.newFieldDefinition()
155+
.name("_empty")
156+
.type(Scalars.GraphQLBoolean)
157+
.build());
162158
}
163159

164160
final Set<GraphQLType> types = new HashSet<>();

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/cache/CacheReader.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
import lombok.extern.slf4j.Slf4j;
1010

1111
@Slf4j
12-
public final class CacheReader {
13-
14-
private CacheReader() {
15-
}
12+
public class CacheReader {
1613

1714
/**
1815
* Response from cache if possible, if nothing in cache will not produce any response
@@ -21,7 +18,7 @@ private CacheReader() {
2118
* found or an error occurred while reading value from cache
2219
* @throws IOException if can not read value from the cache
2320
*/
24-
public static boolean responseFromCache(GraphQLInvocationInput invocationInput,
21+
public boolean responseFromCache(GraphQLInvocationInput invocationInput,
2522
HttpServletRequest request,
2623
HttpServletResponse response,
2724
GraphQLResponseCacheManager cacheManager) throws IOException {
@@ -40,7 +37,7 @@ public static boolean responseFromCache(GraphQLInvocationInput invocationInput,
4037
return false;
4138
}
4239

43-
private static void write(HttpServletResponse response, CachedResponse cachedResponse)
40+
private void write(HttpServletResponse response, CachedResponse cachedResponse)
4441
throws IOException {
4542
if (cachedResponse.isError()) {
4643
response.sendError(cachedResponse.getErrorStatusCode(), cachedResponse.getErrorMessage());

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/cache/CachingHttpRequestInvoker.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@
99
import java.io.IOException;
1010
import javax.servlet.http.HttpServletRequest;
1111
import javax.servlet.http.HttpServletResponse;
12+
import lombok.AccessLevel;
13+
import lombok.RequiredArgsConstructor;
1214
import lombok.extern.slf4j.Slf4j;
1315

1416
@Slf4j
17+
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
1518
public class CachingHttpRequestInvoker implements HttpRequestInvoker {
1619

1720
private final GraphQLConfiguration configuration;
1821
private final HttpRequestInvoker requestInvoker;
22+
private final CacheReader cacheReader;
1923

2024
public CachingHttpRequestInvoker(GraphQLConfiguration configuration) {
21-
this.configuration = configuration;
22-
requestInvoker = new HttpRequestInvokerImpl(configuration, configuration.getGraphQLInvoker(),
23-
new CachingQueryResponseWriterFactory());
25+
this(configuration, new HttpRequestInvokerImpl(configuration, configuration.getGraphQLInvoker(),
26+
new CachingQueryResponseWriterFactory()), new CacheReader());
2427
}
2528

29+
/**
30+
* Try to return value from cache if cache exists, otherwise process the query normally
31+
*/
2632
@Override
2733
public void execute(GraphQLInvocationInput invocationInput, HttpServletRequest request,
2834
HttpServletResponse response) {
29-
// try to return value from cache if cache exists, otherwise processed the query
30-
boolean returnedFromCache;
31-
3235
try {
33-
returnedFromCache = !CacheReader.responseFromCache(
36+
if (!cacheReader.responseFromCache(
3437
invocationInput, request, response, configuration.getResponseCacheManager()
35-
);
38+
)) {
39+
requestInvoker.execute(invocationInput, request, response);
40+
}
3641
} catch (IOException e) {
3742
response.setStatus(STATUS_BAD_REQUEST);
3843
log.warn("Unexpected error happened during response from cache", e);
39-
return;
40-
}
41-
42-
if (!returnedFromCache) {
43-
requestInvoker.execute(invocationInput, request, response);
4444
}
4545
}
4646

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package graphql.kickstart.servlet.cache
2+
3+
import graphql.kickstart.execution.input.GraphQLInvocationInput
4+
import graphql.kickstart.servlet.GraphQLConfiguration
5+
import graphql.kickstart.servlet.HttpRequestInvoker
6+
import spock.lang.Specification
7+
8+
import javax.servlet.http.HttpServletRequest
9+
import javax.servlet.http.HttpServletResponse
10+
11+
class CachingHttpRequestInvokerTest extends Specification {
12+
13+
def cacheReaderMock
14+
def cachingInvoker
15+
def invocationInputMock
16+
def requestMock
17+
def responseMock
18+
def responseCacheManagerMock
19+
def httpRequestInvokerMock
20+
21+
def setup() {
22+
cacheReaderMock = Mock(CacheReader)
23+
invocationInputMock = Mock(GraphQLInvocationInput)
24+
requestMock = Mock(HttpServletRequest)
25+
responseMock = Mock(HttpServletResponse)
26+
responseCacheManagerMock = Mock(GraphQLResponseCacheManager)
27+
def configuration = Mock(GraphQLConfiguration)
28+
httpRequestInvokerMock = Mock(HttpRequestInvoker)
29+
cachingInvoker = new CachingHttpRequestInvoker(configuration, httpRequestInvokerMock, cacheReaderMock)
30+
31+
configuration.getResponseCacheManager() >> responseCacheManagerMock
32+
}
33+
34+
def "should execute regular invoker if cache not exists"() {
35+
given:
36+
cacheReaderMock.responseFromCache(invocationInputMock, requestMock, responseMock, responseCacheManagerMock) >> false
37+
38+
when:
39+
cachingInvoker.execute(invocationInputMock, requestMock, responseMock)
40+
41+
then:
42+
1 * httpRequestInvokerMock.execute(invocationInputMock, requestMock, responseMock)
43+
}
44+
45+
def "should not execute regular invoker if cache exists"() {
46+
given:
47+
cacheReaderMock.responseFromCache(invocationInputMock, requestMock, responseMock, responseCacheManagerMock) >> true
48+
49+
when:
50+
cachingInvoker.execute(invocationInputMock, requestMock, responseMock)
51+
52+
then:
53+
0 * httpRequestInvokerMock.execute(invocationInputMock, requestMock, responseMock)
54+
}
55+
56+
}

0 commit comments

Comments
 (0)