Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public CompletableFuture<PreparsedDocumentEntry> getDocumentAsync(ExecutionInput
try {
if (queryIdOption.isPresent()) {
Object persistedQueryId = queryIdOption.get();
// query text sent along with the id has to match it, even when the id is already cached
String requestQueryText = executionInput.getQuery();
if (hasQueryText(requestQueryText) && persistedQueryIdIsInvalid(persistedQueryId, requestQueryText)) {
throw new PersistedQueryIdInvalid(persistedQueryId);
}
return persistedQueryCache.getPersistedQueryDocumentAsync(persistedQueryId, executionInput, (queryText) -> {
// we have a miss and they gave us nothing - bah!
if (queryText == null || queryText.isBlank()) {
Expand Down Expand Up @@ -79,6 +84,10 @@ protected boolean persistedQueryIdIsInvalid(Object persistedQueryId, String quer
return false;
}

private static boolean hasQueryText(String queryText) {
return queryText != null && !queryText.isBlank() && !queryText.equals(PERSISTED_QUERY_MARKER);
}

/**
* Allows you to customize the graphql error that is sent back on a missing persisted query
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package graphql.execution.preparsed.persisted

import graphql.ExecutionInput
import graphql.GraphQL
import graphql.TestUtil
import graphql.execution.preparsed.PreparsedDocumentEntry
import graphql.parser.Parser
import graphql.schema.DataFetcher
import spock.lang.Specification

import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -100,12 +103,12 @@ class ApolloPersistedQuerySupportTest extends Specification {
persistedQueryCache.parseCount.size() == 0
}

def "will use query hash in preference to query text"() {
def "will use query hash when the query text matches it"() {
CacheImplementation persistedQueryCache = new CacheImplementation()
def apolloSupport = new ApolloPersistedQuerySupport(persistedQueryCache)

when:
def ei = mkEI(hashOne, "{normal}")
def ei = mkEI(hashOne, "query { oneTwoThree }")
def documentEntry = apolloSupport.getDocumentAsync(ei, engineParser).join()
def doc = documentEntry.getDocument()
then:
Expand All @@ -115,6 +118,42 @@ class ApolloPersistedQuerySupportTest extends Specification {

}

def "will have error if the query text does not match the query hash"() {
CacheImplementation persistedQueryCache = new CacheImplementation()
def apolloSupport = new ApolloPersistedQuerySupport(persistedQueryCache)

when:
def ei = mkEI(hashOne, "{normal}")
def documentEntry = apolloSupport.getDocumentAsync(ei, engineParser).join()
then:
documentEntry.getDocument() == null
def error = documentEntry.getErrors()[0]
error.message == "PersistedQueryIdInvalid"
error.getExtensions()["persistedQueryId"] == hashOne
persistedQueryCache.keyCount[hashOne] == null
}

def "will have error if the query text does not match a query hash that is already cached"() {
def schema = TestUtil.schema("type Query { public : String secret : String }",
[Query: [public: { env -> "public" } as DataFetcher, secret: { env -> "secret" } as DataFetcher]])
def cache = new InMemoryPersistedQueryCache([:])
def graphQL = GraphQL.newGraphQL(schema).preparsedDocumentProvider(new ApolloPersistedQuerySupport(cache)).build()
// sha256 of "{ secret }"
def secretHash = "d039e560fffc8b5c8f910b1e30d0ede1df97a362ad5f0c25738f6d4ffd431dc4"

when:
def er = graphQL.execute(mkEI(secretHash, "{ secret }"))
then:
er.errors.isEmpty()
er.data == [secret: "secret"]

when:
er = graphQL.execute(mkEI(secretHash, "{ public }"))
then:
er.data == null
er.errors[0].message == "PersistedQueryIdInvalid"
}

def "will have error if we dont return any query text on cache miss"() {
CacheImplementation persistedQueryCache = new CacheImplementation()

Expand Down
Loading