From 28ec095299814e34aaf0c0780a75fa7542531a0e Mon Sep 17 00:00:00 2001 From: Nabiha Date: Fri, 2 Oct 2026 22:39:49 +0530 Subject: [PATCH] let known queries take precedence in InMemoryPersistedQueryCache --- .../InMemoryPersistedQueryCache.java | 25 +++++++++++++------ .../InMemoryPersistedQueryCacheTest.groovy | 21 ++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/main/java/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCache.java b/src/main/java/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCache.java index 18000e5090..3f6c6f54be 100644 --- a/src/main/java/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCache.java +++ b/src/main/java/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCache.java @@ -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; @@ -37,13 +38,7 @@ public CompletableFuture 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); } @@ -52,6 +47,22 @@ public CompletableFuture 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(); } diff --git a/src/test/groovy/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCacheTest.groovy b/src/test/groovy/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCacheTest.groovy index bffc4e7d9c..4c668536e4 100644 --- a/src/test/groovy/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCacheTest.groovy +++ b/src/test/groovy/graphql/execution/preparsed/persisted/InMemoryPersistedQueryCacheTest.groovy @@ -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}" + } }