-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Persisted query support in graphql-java #2013
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
Merged
Merged
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
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
51 changes: 51 additions & 0 deletions
51
src/main/java/graphql/execution/preparsed/persisted/ApolloPersistedQuerySupport.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,51 @@ | ||
| package graphql.execution.preparsed.persisted; | ||
|
|
||
| import graphql.ExecutionInput; | ||
| import graphql.PublicApi; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * This persisted query support class supports the Apollo scheme where the persisted | ||
| * query id is in {@link graphql.ExecutionInput#getExtensions()}. | ||
| * <p> | ||
| * You need to provide a {@link PersistedQueryCache} cache implementation | ||
| * as the backing cache. | ||
| * <p> | ||
| * See <a href="https://www.apollographql.com/docs/apollo-server/performance/apq/">Apollo Persisted Queries</a> | ||
| * <p> | ||
| * The Apollo client sends a hash of the persisted query in the input extensions in the following form | ||
| * <p> | ||
| * <pre> | ||
| * { | ||
| * "extensions":{ | ||
| * "persistedQuery":{ | ||
| * "version":1, | ||
| * "sha256Hash":"fcf31818e50ac3e818ca4bdbc433d6ab73176f0b9d5f9d5ad17e200cdab6fba4" | ||
| * } | ||
| * } | ||
| * } | ||
| * </pre> | ||
| * | ||
| * @see graphql.ExecutionInput#getExtensions() | ||
| */ | ||
| @PublicApi | ||
| public class ApolloPersistedQuerySupport extends PersistedQuerySupport { | ||
|
|
||
| public ApolloPersistedQuerySupport(PersistedQueryCache persistedQueryCache) { | ||
| super(persistedQueryCache); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| protected Optional<Object> getPersistedQueryId(ExecutionInput executionInput) { | ||
| Map<String, Object> extensions = executionInput.getExtensions(); | ||
| Map<String, Object> persistedQuery = (Map<String, Object>) extensions.get("persistedQuery"); | ||
| if (persistedQuery != null) { | ||
| Object sha256Hash = persistedQuery.get("sha256Hash"); | ||
| return Optional.ofNullable(sha256Hash); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
Member
Author
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. Later if there is a V2, we can update this |
||
59 changes: 59 additions & 0 deletions
59
src/main/java/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCache.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,59 @@ | ||
| package graphql.execution.preparsed.persisted; | ||
|
|
||
| import graphql.Assert; | ||
| import graphql.ExecutionInput; | ||
| import graphql.PublicApi; | ||
| import graphql.execution.preparsed.PreparsedDocumentEntry; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * A PersistedQueryCache that is just an in memory map of known queries. | ||
| */ | ||
| @PublicApi | ||
| public class InMemoryPersistedQueryCache implements PersistedQueryCache { | ||
|
|
||
| private final Map<Object, PreparsedDocumentEntry> cache = new ConcurrentHashMap<>(); | ||
| private final Map<Object, String> knownQueries; | ||
|
|
||
| public InMemoryPersistedQueryCache(Map<Object, String> knownQueries) { | ||
| this.knownQueries = Assert.assertNotNull(knownQueries); | ||
| } | ||
|
|
||
| public Map<Object, String> getKnownQueries() { | ||
| return knownQueries; | ||
| } | ||
|
|
||
| @Override | ||
| public PreparsedDocumentEntry getPersistedQueryDocument(Object persistedQueryId, ExecutionInput executionInput, PersistedQueryCacheMiss onCacheMiss) throws PersistedQueryNotFound { | ||
| return cache.compute(persistedQueryId, (k, v) -> { | ||
| if (v != null) { | ||
| return v; | ||
| } | ||
| String queryText = knownQueries.get(persistedQueryId); | ||
| if (queryText == null) { | ||
| throw new PersistedQueryNotFound(persistedQueryId); | ||
| } | ||
| return onCacheMiss.apply(queryText); | ||
| }); | ||
| } | ||
|
|
||
| public static Builder newInMemoryPersistedQueryCache() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final Map<Object, String> knownQueries = new HashMap<>(); | ||
|
|
||
| public Builder addQuery(Object key, String queryText) { | ||
| knownQueries.put(key, queryText); | ||
| return this; | ||
| } | ||
|
|
||
| public InMemoryPersistedQueryCache build() { | ||
| return new InMemoryPersistedQueryCache(knownQueries); | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/graphql/execution/preparsed/persisted/PersistedQueryCache.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,31 @@ | ||
| package graphql.execution.preparsed.persisted; | ||
|
|
||
| import graphql.ExecutionInput; | ||
| import graphql.PublicSpi; | ||
| import graphql.execution.preparsed.PreparsedDocumentEntry; | ||
|
|
||
| /** | ||
| * This interface is used to abstract an actual cache that can cache parsed persistent queries. | ||
| */ | ||
| @PublicSpi | ||
| public interface PersistedQueryCache { | ||
|
|
||
| /** | ||
| * This is called to get a persisted query from cache. | ||
| * <p> | ||
| * If its present in cache then it must return a PreparsedDocumentEntry where {@link graphql.execution.preparsed.PreparsedDocumentEntry#getDocument()} | ||
| * is already parsed and validated. This will be passed onto the graphql engine as is. | ||
| * <p> | ||
| * If its a valid query id but its no present in cache, (cache miss) then you need to call back the "onCacheMiss" function with associated query text. | ||
| * This will be compiled and validated by the graphql engine and the a PreparsedDocumentEntry will be passed back ready for you to cache it. | ||
| * <p> | ||
| * If its not a valid query id then throw a {@link graphql.execution.preparsed.persisted.PersistedQueryNotFound} to indicate this. | ||
| * | ||
| * @param persistedQueryId the persisted query id | ||
| * @param executionInput the original execution input | ||
| * @param onCacheMiss the call back should it be a valid query id but its not currently not in the cache | ||
| * @return a parsed and validated PreparsedDocumentEntry where {@link graphql.execution.preparsed.PreparsedDocumentEntry#getDocument()} is set | ||
| * @throws graphql.execution.preparsed.persisted.PersistedQueryNotFound if the query id is not know at all and you have no query text | ||
| */ | ||
| PreparsedDocumentEntry getPersistedQueryDocument(Object persistedQueryId, ExecutionInput executionInput, PersistedQueryCacheMiss onCacheMiss) throws PersistedQueryNotFound; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/graphql/execution/preparsed/persisted/PersistedQueryCacheMiss.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,23 @@ | ||
| package graphql.execution.preparsed.persisted; | ||
|
|
||
| import graphql.PublicApi; | ||
| import graphql.execution.preparsed.PreparsedDocumentEntry; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * The call back when a valid persisted query is not in cache and it needs to be compiled and validated | ||
| * by the graphql engine. If you get a cache miss in your {@link graphql.execution.preparsed.persisted.PersistedQueryCache} implementation | ||
| * then you are required to call back on the provided instance of this interface | ||
| */ | ||
| @PublicApi | ||
| public interface PersistedQueryCacheMiss extends Function<String, PreparsedDocumentEntry> { | ||
| /** | ||
| * You give back the missing query text and graphql-java will compile and validate it. | ||
| * | ||
| * @param queryToBeParsedAndValidated the query text to be parsed and validated | ||
| * @return a parsed and validated query document ready for caching | ||
| */ | ||
| @Override | ||
| PreparsedDocumentEntry apply(String queryToBeParsedAndValidated); | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/graphql/execution/preparsed/persisted/PersistedQueryNotFound.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,40 @@ | ||
| package graphql.execution.preparsed.persisted; | ||
|
|
||
| import graphql.ErrorClassification; | ||
| import graphql.PublicApi; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * An exception that indicates the query id is not valid and can be found ever in cache | ||
| */ | ||
| @PublicApi | ||
| public class PersistedQueryNotFound extends RuntimeException implements ErrorClassification { | ||
| private final Object persistedQueryId; | ||
|
|
||
| public PersistedQueryNotFound(Object persistedQueryId) { | ||
| this.persistedQueryId = persistedQueryId; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return "PersistedQueryNotFound"; | ||
| } | ||
|
|
||
| public Object getPersistedQueryId() { | ||
| return persistedQueryId; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "PersistedQueryNotFound"; | ||
| } | ||
|
|
||
| public Map<String, Object> getExtensions() { | ||
| LinkedHashMap<String, Object> extensions = new LinkedHashMap<>(); | ||
| extensions.put("persistedQueryId", persistedQueryId); | ||
| extensions.put("generatedBy", "graphql-java"); | ||
| return extensions; | ||
| } | ||
| } |
Oops, something went wrong.
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.
This is a much better name for the callback parameter because that's what it does