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 @@ -6,6 +6,7 @@
import graphql.execution.preparsed.PreparsedDocumentEntry;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -37,13 +38,7 @@ public CompletableFuture<PreparsedDocumentEntry> getPersistedQueryDocumentAsync(
return v;
}

//get the query from the execution input. Make sure it's not null, empty or the APQ marker.
// if it is, fallback to the known queries.
String queryText = executionInput.getQuery();
if (queryText == null || queryText.isEmpty() || queryText.equals(PersistedQuerySupport.PERSISTED_QUERY_MARKER)) {
queryText = knownQueries.get(persistedQueryId);
}

String queryText = getQueryText(persistedQueryId, executionInput);
if (queryText == null) {
throw new PersistedQueryNotFound(persistedQueryId);
}
Expand All @@ -52,6 +47,22 @@ public CompletableFuture<PreparsedDocumentEntry> getPersistedQueryDocumentAsync(
return CompletableFuture.completedFuture(documentEntry);
}

private @Nullable String getQueryText(Object persistedQueryId, ExecutionInput executionInput) {
// the known queries are the source of truth for an id, so they take precedence over the
// query text in the execution input. Only an id that is not known can be registered from the input.
String knownQueryText = knownQueries.get(persistedQueryId);
if (knownQueryText != null) {
return knownQueryText;
}

//get the query from the execution input. Make sure it's not null, empty or the APQ marker.
String queryText = executionInput.getQuery();
if (queryText == null || queryText.isEmpty() || queryText.equals(PersistedQuerySupport.PERSISTED_QUERY_MARKER)) {
return null;
}
return queryText;
}

public static Builder newInMemoryPersistedQueryCache() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,25 @@ class InMemoryPersistedQueryCacheTest extends Specification {
then:
printAstCompact(doc) == "{foo bar baz}"
}

def "known queries take precedence over the query text in the execution input"() {
def hash = "knownhash"
def inMemCache = InMemoryPersistedQueryCache.newInMemoryPersistedQueryCache()
.addQuery(hash, "{foo bar baz}")
.build()
def ei = mkEI(hash, "{ somethingElse }")

when:
def getDoc = inMemCache.getPersistedQueryDocumentAsync(hash, ei, onMiss).join()
def doc = getDoc.document
then:
printAstCompact(doc) == "{foo bar baz}"

// a later request that only sends the id must not see the text from the earlier request
when:
def laterEi = mkEI(hash, PersistedQuerySupport.PERSISTED_QUERY_MARKER)
def laterDoc = inMemCache.getPersistedQueryDocumentAsync(hash, laterEi, onMiss).join().document
then:
printAstCompact(laterDoc) == "{foo bar baz}"
}
}
Loading