-
Notifications
You must be signed in to change notification settings - Fork 301
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
User configured PreParsedDocumentProvider should be used for regular queries when APQ feature is enabled. #2128
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af44d5d
APQ enhancements.
srinivasankavitha 05aa4cb
Clean up code and add tests.
srinivasankavitha 022d23b
Remove temp code.
srinivasankavitha 4503d97
Cleanup.
srinivasankavitha a1b1909
Fix ordering of instrumentation such that the metrics instrumentation…
srinivasankavitha 4b1afe2
Undo change to add order.
srinivasankavitha b2dc0cc
Address review feedback.
srinivasankavitha 70c84af
Fix the ordering of instrumentation classes to be run.
srinivasankavitha 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 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 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 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 |
---|---|---|
|
@@ -20,4 +20,8 @@ management: | |
exposure: | ||
include: | ||
- metrics | ||
debug: true | ||
debug: true | ||
dgs: | ||
graphql: | ||
apq: | ||
enabled: true |
47 changes: 47 additions & 0 deletions
47
...hql/src/main/kotlin/com/netflix/graphql/dgs/apq/DgsAPQPreParsedDocumentProviderWrapper.kt
This file contains 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,47 @@ | ||
/* | ||
* Copyright 2025 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.apq | ||
|
||
import graphql.ExecutionInput | ||
import graphql.execution.preparsed.PreparsedDocumentEntry | ||
import graphql.execution.preparsed.PreparsedDocumentProvider | ||
import graphql.execution.preparsed.persisted.ApolloPersistedQuerySupport | ||
import graphql.execution.preparsed.persisted.PersistedQueryCache | ||
import java.util.* | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.function.Function | ||
|
||
class DgsAPQPreParsedDocumentProviderWrapper( | ||
persistedQueryCache: PersistedQueryCache, | ||
private val preparsedDocumentProvider: Optional<PreparsedDocumentProvider>, | ||
) : ApolloPersistedQuerySupport(persistedQueryCache) { | ||
override fun getDocumentAsync( | ||
executionInput: ExecutionInput, | ||
parseAndValidateFunction: Function<ExecutionInput, PreparsedDocumentEntry>, | ||
): CompletableFuture<PreparsedDocumentEntry> { | ||
val queryId = getPersistedQueryId(executionInput) | ||
if (queryId.isPresent) { | ||
return super.getDocumentAsync(executionInput, parseAndValidateFunction) | ||
} | ||
|
||
if (preparsedDocumentProvider.isPresent) { | ||
return preparsedDocumentProvider.get().getDocumentAsync(executionInput, parseAndValidateFunction) | ||
} | ||
|
||
return CompletableFuture.completedFuture(parseAndValidateFunction.apply(executionInput)) | ||
} | ||
} |
This file contains 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 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
127 changes: 127 additions & 0 deletions
127
...n/com/netflix/graphql/dgs/springgraphql/apq/DgsAPQPreParsedDocumentProviderWrapperTest.kt
This file contains 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,127 @@ | ||
/* | ||
* Copyright 2025 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.springgraphql.apq | ||
|
||
import com.netflix.graphql.dgs.apq.DgsAPQPreParsedDocumentProviderWrapper | ||
import graphql.ExecutionInput | ||
import graphql.execution.preparsed.PreparsedDocumentEntry | ||
import graphql.execution.preparsed.PreparsedDocumentProvider | ||
import graphql.execution.preparsed.persisted.PersistedQueryCache | ||
import graphql.execution.preparsed.persisted.PersistedQuerySupport.PERSISTED_QUERY_MARKER | ||
import graphql.language.Document | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.mockk | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import java.util.* | ||
import java.util.concurrent.CompletableFuture | ||
|
||
@ExtendWith(MockKExtension::class) | ||
class DgsAPQPreParsedDocumentProviderWrapperTest { | ||
@Autowired | ||
private lateinit var dgsAPQPreParsedDocumentProvider: DgsAPQPreParsedDocumentProviderWrapper | ||
|
||
@MockK | ||
lateinit var preparsedDocumentProvider: PreparsedDocumentProvider | ||
|
||
@MockK | ||
lateinit var persistedQueryCache: PersistedQueryCache | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
dgsAPQPreParsedDocumentProvider = | ||
DgsAPQPreParsedDocumentProviderWrapper(persistedQueryCache, Optional.of(preparsedDocumentProvider)) | ||
} | ||
|
||
@Test | ||
fun `APQ only queries with just the hash use the persisted query cache`() { | ||
var count = 0 | ||
val document = mockk<Document>() | ||
val computeFunction = { _: ExecutionInput -> | ||
count++ | ||
PreparsedDocumentEntry(document) | ||
} | ||
val extensions: MutableMap<String, Any> = HashMap() | ||
extensions["persistedQuery"] = | ||
mapOf("version" to "1", "sha256Hash" to "ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38") | ||
val executionInput = | ||
ExecutionInput | ||
.Builder() | ||
.query(PERSISTED_QUERY_MARKER) | ||
.extensions(extensions) | ||
.build() | ||
|
||
every { | ||
persistedQueryCache.getPersistedQueryDocumentAsync(any(), any(), any()) | ||
}.returns(CompletableFuture.completedFuture(computeFunction(executionInput))) | ||
|
||
dgsAPQPreParsedDocumentProvider.getDocumentAsync(executionInput, computeFunction) | ||
assertThat(count).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun `APQ queries with query and hash use the persisted query cache`() { | ||
var count = 0 | ||
val document = mockk<Document>() | ||
val computeFunction = { _: ExecutionInput -> | ||
count++ | ||
PreparsedDocumentEntry(document) | ||
} | ||
val extensions: MutableMap<String, Any> = HashMap() | ||
extensions["persistedQuery"] = | ||
mapOf("version" to "1", "sha256Hash" to "ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38") | ||
val executionInput = | ||
ExecutionInput | ||
.Builder() | ||
.query("{__typename}") | ||
.extensions(extensions) | ||
.build() | ||
|
||
every { | ||
persistedQueryCache.getPersistedQueryDocumentAsync(any(), any(), any()) | ||
}.returns(CompletableFuture.completedFuture(computeFunction(executionInput))) | ||
|
||
dgsAPQPreParsedDocumentProvider.getDocumentAsync(executionInput, computeFunction) | ||
assertThat(count).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun `Plain queries (non-APQ) use the user specified preparsed document provider`() { | ||
var count = 0 | ||
val document = mockk<Document>() | ||
val computeFunction = { _: ExecutionInput -> | ||
count++ | ||
PreparsedDocumentEntry(document) | ||
} | ||
val extensions: MutableMap<String, Any> = HashMap() | ||
extensions["persistedQuery"] = | ||
mapOf("version" to "1", "sha256Hash" to "ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38") | ||
val executionInput = ExecutionInput.Builder().query("{__typename}").build() | ||
|
||
every { | ||
preparsedDocumentProvider.getDocumentAsync(executionInput, any()) | ||
}.returns(CompletableFuture.completedFuture(computeFunction(executionInput))) | ||
|
||
dgsAPQPreParsedDocumentProvider.getDocumentAsync(executionInput, computeFunction) | ||
assertThat(count).isEqualTo(1) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...st/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/DgsAPQAutoConfigurationTest.kt
This file contains 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,63 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.springgraphql.autoconfig | ||
|
||
import com.netflix.graphql.dgs.apq.DgsAPQSupportAutoConfiguration | ||
import graphql.execution.preparsed.persisted.PersistedQueryCache | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.autoconfigure.AutoConfigurations | ||
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner | ||
|
||
class DgsAPQAutoConfigurationTest { | ||
private val autoConfigurations = | ||
AutoConfigurations.of( | ||
DgsSpringGraphQLAutoConfiguration::class.java, | ||
DgsAPQSupportAutoConfiguration::class.java, | ||
GraphQlAutoConfiguration::class.java, | ||
) | ||
|
||
@Test | ||
fun shouldContributeAPQBeans() { | ||
val contextRunner = | ||
ApplicationContextRunner() | ||
.withConfiguration(autoConfigurations) | ||
.withPropertyValues("dgs.graphql.apq.enabled=true") | ||
|
||
contextRunner.run { context -> | ||
assertThat(context) | ||
.hasBean("apqSourceBuilderCustomizer") | ||
.hasBean("sourceBuilderCustomizer") | ||
.hasSingleBean(PersistedQueryCache::class.java) | ||
} | ||
} | ||
|
||
@Test | ||
fun shouldNotContributeAPQBeans() { | ||
val contextRunner = | ||
ApplicationContextRunner() | ||
.withConfiguration(autoConfigurations) | ||
|
||
contextRunner.run { context -> | ||
assertThat(context) | ||
.doesNotHaveBean("apqSourceBuilderCustomizer") | ||
.hasBean("sourceBuilderCustomizer") | ||
.doesNotHaveBean(PersistedQueryCache::class.java) | ||
} | ||
} | ||
} |
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.
Why did this change?
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.
explained in response to earlier comment
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.
The micrometer test started failing because the order in which the instrumentations run seems to have changed in my branch. If the
DgsGraphQlJavaErrorInstrumentation
runs before theMetricsInstrumentation
, the tag value is set properly (which is in this branch). However, if it's the other way around, the metrics add the tag for gql.error before theDgsGraphQlJavaErrorInstrumentation
runs and that's why it passes in master branch.The correct order should be
DgsGraphQlJavaErrorInstrumentation
first and thenDgsMetricsInstrumentation
so the metrics get the correct tag value as fixed in this PR.Basically need a way to ensure the MetricsInstrumentation runs last, but not sure what the best way to do that is, so looking into it.