Skip to content

Commit

Permalink
Merge pull request #1732 from Netflix/ejd--preparsed-document-provider
Browse files Browse the repository at this point in the history
add easily enabled/configured PrecachedDocumentProvider implementation
  • Loading branch information
Emily authored Dec 6, 2023
2 parents c586117 + 48414c6 commit 969b721
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.graphql.dgs.autoconfig

import com.netflix.graphql.dgs.DgsDataLoaderOptionsProvider
import com.netflix.graphql.dgs.DgsDefaultPreparsedDocumentProvider
import com.netflix.graphql.dgs.DgsFederationResolver
import com.netflix.graphql.dgs.DgsQueryExecutor
import com.netflix.graphql.dgs.conditionals.ConditionalOnJava21
Expand Down Expand Up @@ -68,6 +69,7 @@ import org.springframework.http.HttpHeaders
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.context.request.WebRequest
import java.time.Duration
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
Expand Down Expand Up @@ -236,6 +238,21 @@ open class DgsAutoConfiguration(
return dgsSchemaProvider.schema(null, fieldVisibility)
}

@Bean
@ConditionalOnProperty(
prefix = "$AUTO_CONF_PREFIX.preparsedDocumentProvider",
name = ["enabled"],
havingValue = "true",
matchIfMissing = false
)
@ConditionalOnMissingBean
open fun preparsedDocumentProvider(configProps: DgsConfigurationProperties): PreparsedDocumentProvider {
return DgsDefaultPreparsedDocumentProvider(
configProps.preparsedDocumentProvider.maximumCacheSize,
Duration.parse(configProps.preparsedDocumentProvider.cacheValidityDuration)
)
}

@Bean
@ConditionalOnProperty(
prefix = "$AUTO_CONF_PREFIX.introspection",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ data class DgsConfigurationProperties(
/** Location of the GraphQL schema files. */
@DefaultValue(DEFAULT_SCHEMA_LOCATION) val schemaLocations: List<String>,
@DefaultValue("true") val schemaWiringValidationEnabled: Boolean,
@DefaultValue("false") val enableEntityFetcherCustomScalarParsing: Boolean
@DefaultValue("false") val enableEntityFetcherCustomScalarParsing: Boolean,
val preparsedDocumentProvider: DgsPreparsedDocumentProviderConfigurationProperties = DgsPreparsedDocumentProviderConfigurationProperties()
) {
data class DgsPreparsedDocumentProviderConfigurationProperties(
val enabled: Boolean = false,
val maximumCacheSize: Long = 2000,
/** How long cache entries are valid for since creation, replacement or last access, specified with an ISO-8601 duration string. **/
val cacheValidityDuration: String = "PT1H"
)

companion object {
const val PREFIX: String = "dgs.graphql"
Expand Down
1 change: 1 addition & 0 deletions graphql-dgs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
compileOnly("org.springframework.security:spring-security-core")
compileOnly("io.projectreactor:reactor-core")
compileOnly("jakarta.annotation:jakarta.annotation-api")
compileOnly("com.github.ben-manes.caffeine:caffeine")

implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 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

import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import graphql.ExecutionInput
import graphql.execution.preparsed.PreparsedDocumentEntry
import graphql.execution.preparsed.PreparsedDocumentProvider
import java.time.Duration
import java.util.function.Function

class DgsDefaultPreparsedDocumentProvider(maximumSize: Long, expireAfterAccess: Duration) : PreparsedDocumentProvider {
val cache: Cache<String, PreparsedDocumentEntry> = Caffeine.newBuilder()
.maximumSize(maximumSize)
.expireAfterAccess(expireAfterAccess)
.build()
override fun getDocument(
executionInput: ExecutionInput,
parseAndValidateFunction: Function<ExecutionInput, PreparsedDocumentEntry>
): PreparsedDocumentEntry {
return cache.get(executionInput.query) { parseAndValidateFunction.apply(executionInput) }
}
}

0 comments on commit 969b721

Please sign in to comment.