-
Notifications
You must be signed in to change notification settings - Fork 68
Serve HTTP cache headers. #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ttyniwa
wants to merge
1
commit into
leangen:master
Choose a base branch
from
ttyniwa:cache-control-header
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
...ot-autoconfigure/src/main/java/io/leangen/graphql/spqr/spring/web/CacheControlHeader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.leangen.graphql.spqr.spring.web; | ||
|
||
import graphql.cachecontrol.CacheControl; | ||
import org.springframework.http.HttpHeaders; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class CacheControlHeader { | ||
|
||
/** | ||
* For any response whose overall cache policy has a non-zero maxAge, This method will automatically set the | ||
* Cache-Control HTTP response header to an appropriate value describing the maxAge and scope, | ||
* such as Cache-Control: max-age=60, private. | ||
* https://www.apollographql.com/docs/apollo-server/features/caching/#serving-http-cache-headers | ||
* | ||
* @param response graphql response. | ||
* @param headers response headers. | ||
*/ | ||
public static void addCacheControlHeader(Object response, HttpHeaders headers) { | ||
CachePolicy cachePolicy; | ||
if (response instanceof CompletableFuture) { | ||
try { | ||
cachePolicy = computeOverallCacheMaxAge(((CompletableFuture<Map<String, Object>>) response).get()); | ||
} catch (ExecutionException | InterruptedException e) { | ||
throw new RuntimeException("", e); | ||
} | ||
} else { | ||
cachePolicy = computeOverallCacheMaxAge((Map<String, Object>) response); | ||
} | ||
|
||
if (cachePolicy != null) { | ||
headers.add(HttpHeaders.CACHE_CONTROL, "max-age=" + cachePolicy.getMaxAge() + ", " + cachePolicy.getScope().name().toLowerCase()); | ||
} | ||
} | ||
|
||
static private <T> T get( | ||
String keyName, | ||
Map<String, Object> executionResult | ||
) { | ||
if (executionResult == null || executionResult.get(keyName) == null) { | ||
return null; | ||
} | ||
return (T) executionResult.get(keyName); | ||
} | ||
|
||
// reference https://github.com/apollographql/apollo-server/blob/d5015f4ea00cadb2a74b09956344e6f65c084629/packages/apollo-cache-control/src/index.ts#L180 | ||
static private CachePolicy computeOverallCacheMaxAge( | ||
Map<String, Object> executionResult | ||
) { | ||
Map<String, Object> extensions = get("extensions", executionResult); | ||
Map<String, Object> cacheControl = get("cacheControl", extensions); | ||
List<Map<Object, Object>> hints = get("hints", cacheControl); | ||
if (hints == null) { | ||
return null; | ||
} | ||
|
||
// find lowest maxAge by hints. | ||
Integer lowestMaxAge = null; | ||
CacheControl.Scope scope = CacheControl.Scope.PUBLIC; | ||
for (Map<Object, Object> hint : hints) { | ||
Integer maxAge = (Integer) hint.get("maxAge"); | ||
lowestMaxAge = lowestMaxAge == null ? maxAge : Math.min(maxAge, lowestMaxAge); | ||
if (CacheControl.Scope.PRIVATE.name().equals(hint.get("scope"))) { | ||
scope = CacheControl.Scope.PRIVATE; | ||
} | ||
} | ||
|
||
// check all data fields has hints. | ||
Map<String, Object> data = get("data", executionResult); | ||
if (data == null) { | ||
return null; | ||
} | ||
boolean isExistHint = data.entrySet().stream() | ||
.allMatch((entry) -> hints.stream() | ||
.anyMatch((it) -> String.join(".", ((List<String>) it.get("path"))).equals(entry.getKey()))); | ||
|
||
// if hints don't exists, then return null(not cacheable). | ||
return isExistHint ? new CachePolicy(lowestMaxAge, scope) : null; | ||
} | ||
|
||
static class CachePolicy { | ||
private Integer maxAge; | ||
private CacheControl.Scope scope; | ||
|
||
CachePolicy(Integer maxAge, CacheControl.Scope scope) { | ||
this.maxAge = maxAge; | ||
this.scope = scope; | ||
} | ||
|
||
public Integer getMaxAge() { | ||
return maxAge; | ||
} | ||
|
||
public CacheControl.Scope getScope() { | ||
return scope; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package io.leangen.graphql.spqr.spring.test; | ||
|
||
import graphql.cachecontrol.CacheControl; | ||
import io.leangen.graphql.ExtensionProvider; | ||
import io.leangen.graphql.GeneratorConfiguration; | ||
import io.leangen.graphql.annotations.GraphQLEnvironment; | ||
import io.leangen.graphql.annotations.GraphQLQuery; | ||
import io.leangen.graphql.execution.ResolutionEnvironment; | ||
import io.leangen.graphql.metadata.strategy.query.BeanResolverBuilder; | ||
import io.leangen.graphql.metadata.strategy.query.PublicResolverBuilder; | ||
import io.leangen.graphql.metadata.strategy.query.ResolverBuilder; | ||
|
@@ -135,6 +138,12 @@ public String getGreeting(){ | |
return "Hello world !"; | ||
} | ||
|
||
@GraphQLQuery(name = "greetingFromBeanSource_wiredAsComponent_byAnnotation_withCacheHint") | ||
public String getGreeting2(@GraphQLEnvironment ResolutionEnvironment env){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remove argument, success to pass |
||
env.dataFetchingEnvironment.getCacheControl().hint(env.dataFetchingEnvironment, 100, CacheControl.Scope.PUBLIC); | ||
return "Hello world !"; | ||
} | ||
|
||
public String greetingFromBeanSource_wiredAsComponent_byStringQualifiedCustomResolverBuilder_wiredAsBean() { | ||
return "Hello world !"; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit Test Fails.
I don't know how to solve it.
Please let me know.