Skip to content

Commit

Permalink
Add new NadelServiceExecutionContext that is not shared between servi…
Browse files Browse the repository at this point in the history
…ces (#568)

* Add new NadelServiceExecutionContext that is not shared between services

* Add test

* Fix test

* Fix deleted line
  • Loading branch information
gnawf committed Aug 13, 2024
1 parent 79b230c commit 41d1b44
Show file tree
Hide file tree
Showing 35 changed files with 553 additions and 6 deletions.
19 changes: 18 additions & 1 deletion lib/src/main/java/graphql/nadel/NextgenEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import graphql.introspection.Introspection.TypeNameMetaFieldDef
import graphql.language.Document
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelIncrementalResultSupport
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.IntrospectionService
import graphql.nadel.engine.blueprint.NadelDefaultIntrospectionRunner
import graphql.nadel.engine.blueprint.NadelExecutionBlueprintFactory
Expand All @@ -36,6 +37,7 @@ import graphql.nadel.engine.util.provide
import graphql.nadel.engine.util.singleOfType
import graphql.nadel.engine.util.strictAssociateBy
import graphql.nadel.hooks.NadelExecutionHooks
import graphql.nadel.hooks.createServiceExecutionContext
import graphql.nadel.instrumentation.NadelInstrumentation
import graphql.nadel.instrumentation.parameters.ErrorData
import graphql.nadel.instrumentation.parameters.ErrorType.ServiceExecutionError
Expand Down Expand Up @@ -257,25 +259,35 @@ internal class NextgenEngine(
service: Service,
executionContext: NadelExecutionContext,
): ServiceExecutionResult {
val serviceExecutionContext = executionHooks.createServiceExecutionContext(service)

val timer = executionContext.timer
val executionPlan = timer.time(step = RootStep.ExecutionPlanning) {
executionPlanner.create(
executionContext = executionContext,
serviceExecutionContext = serviceExecutionContext,
services = services,
service = service,
rootField = topLevelField,
serviceHydrationDetails = executionContext.hydrationDetails,
)
}
val queryTransform = timer.time(step = RootStep.QueryTransforming) {
transformQuery(service, executionContext, executionPlan, topLevelField)
transformQuery(
service = service,
executionContext = executionContext,
serviceExecutionContext = serviceExecutionContext,
executionPlan = executionPlan,
field = topLevelField
)
}
val transformedQuery = queryTransform.result.single()
val result: ServiceExecutionResult = timer.time(step = RootStep.ServiceExecution.child(service.name)) {
executeService(
service = service,
transformedQuery = transformedQuery,
executionContext = executionContext,
serviceExecutionContext = serviceExecutionContext,
executionHydrationDetails = executionContext.hydrationDetails,
)
}
Expand All @@ -284,6 +296,7 @@ internal class NextgenEngine(
else -> timer.time(step = RootStep.ResultTransforming) {
resultTransformer.transform(
executionContext = executionContext,
serviceExecutionContext = serviceExecutionContext,
executionPlan = executionPlan,
artificialFields = queryTransform.artificialFields,
overallToUnderlyingFields = queryTransform.overallToUnderlyingFields,
Expand All @@ -300,6 +313,7 @@ internal class NextgenEngine(
service: Service,
transformedQuery: ExecutableNormalizedField,
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionHydrationDetails: ServiceExecutionHydrationDetails? = null,
): ServiceExecutionResult {
val timer = executionContext.timer
Expand Down Expand Up @@ -327,6 +341,7 @@ internal class NextgenEngine(
variables = compileResult.variables,
operationDefinition = compileResult.document.definitions.singleOfType(),
serviceContext = executionContext.getContextForService(service).await(),
serviceExecutionContext = serviceExecutionContext,
hydrationDetails = executionHydrationDetails,
executableNormalizedField = transformedQuery,
)
Expand Down Expand Up @@ -424,13 +439,15 @@ internal class NextgenEngine(
private suspend fun transformQuery(
service: Service,
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionPlan: NadelExecutionPlan,
field: ExecutableNormalizedField,
): NadelQueryTransformer.TransformResult {
return NadelQueryTransformer.transformQuery(
overallExecutionBlueprint,
service,
executionContext,
serviceExecutionContext,
executionPlan,
field,
)
Expand Down
3 changes: 3 additions & 0 deletions lib/src/main/java/graphql/nadel/ServiceExecutionParameters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.GraphQLContext
import graphql.execution.ExecutionId
import graphql.language.Document
import graphql.language.OperationDefinition
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.normalized.ExecutableNormalizedField

class ServiceExecutionParameters internal constructor(
Expand All @@ -13,13 +14,15 @@ class ServiceExecutionParameters internal constructor(
val variables: Map<String, Any>,
val operationDefinition: OperationDefinition,
val executionId: ExecutionId,
val serviceExecutionContext: NadelServiceExecutionContext,
private val serviceContext: Any?,
/**
* @return details abut this service hydration or null if it's not a hydration call
*/
val hydrationDetails: ServiceExecutionHydrationDetails?,
val executableNormalizedField: ExecutableNormalizedField,
) {
@Deprecated("Use serviceExecutionContext instead")
fun <T> getServiceContext(): T? {
@Suppress("UNCHECKED_CAST") // Trust caller
return serviceContext as T?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data class NadelExecutionContext internal constructor(
/**
* Get the service context for a given service
*/
@Deprecated("Replaced with NadelServiceExecutionContext")
fun getContextForService(service: Service): CompletableFuture<Any?> {
return serviceContexts.getOrPut(service.name) {
hooks.createServiceContext(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package graphql.nadel.engine

/**
* Base class for context objects created for one service execution.
*
* This is created for each portion of the query we execute.
*
* This is NOT shared with other executions to the same service.
*/
abstract class NadelServiceExecutionContext {
internal object None : NadelServiceExecutionContext()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.nadel.NextgenEngine
import graphql.nadel.Service
import graphql.nadel.ServiceExecutionHydrationDetails
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.NadelOverallExecutionBlueprint
import graphql.nadel.engine.transform.NadelDeepRenameTransform
import graphql.nadel.engine.transform.NadelRenameArgumentInputTypesTransform
Expand Down Expand Up @@ -35,6 +36,7 @@ internal class NadelExecutionPlanFactory(
*/
suspend fun create(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
services: Map<String, Service>,
service: Service,
rootField: ExecutableNormalizedField,
Expand All @@ -55,6 +57,7 @@ internal class NadelExecutionPlanFactory(
val state = timer.time(step = timingStep) {
transform.isApplicable(
executionContext,
serviceExecutionContext,
executionBlueprint,
services,
service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.nadel.Service
import graphql.nadel.ServiceExecutionHydrationDetails
import graphql.nadel.ServiceExecutionResult
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.NadelDeepRenameFieldInstruction
import graphql.nadel.engine.blueprint.NadelOverallExecutionBlueprint
import graphql.nadel.engine.blueprint.getTypeNameToInstructionMap
Expand Down Expand Up @@ -82,6 +83,7 @@ internal class NadelDeepRenameTransform : NadelTransform<NadelDeepRenameTransfor
*/
override suspend fun isApplicable(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
services: Map<String, Service>,
service: Service,
Expand Down Expand Up @@ -153,6 +155,7 @@ internal class NadelDeepRenameTransform : NadelTransform<NadelDeepRenameTransfor
*/
override suspend fun transformField(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
transformer: NadelQueryTransformer,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
Expand Down Expand Up @@ -286,10 +289,11 @@ internal class NadelDeepRenameTransform : NadelTransform<NadelDeepRenameTransfor
*/
override suspend fun getResultInstructions(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
overallField: ExecutableNormalizedField,
underlyingParentField: ExecutableNormalizedField?, // Overall field
underlyingParentField: ExecutableNormalizedField?,
result: ServiceExecutionResult,
state: State,
nodes: JsonNodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.nadel.Service
import graphql.nadel.ServiceExecutionHydrationDetails
import graphql.nadel.ServiceExecutionResult
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.NadelOverallExecutionBlueprint
import graphql.nadel.engine.transform.NadelRenameArgumentInputTypesTransform.State
import graphql.nadel.engine.transform.query.NadelQueryTransformer
Expand All @@ -29,6 +30,7 @@ internal class NadelRenameArgumentInputTypesTransform : NadelTransform<State> {

override suspend fun isApplicable(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
services: Map<String, Service>,
service: Service,
Expand All @@ -47,6 +49,7 @@ internal class NadelRenameArgumentInputTypesTransform : NadelTransform<State> {

override suspend fun transformField(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
transformer: NadelQueryTransformer,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
Expand All @@ -63,6 +66,7 @@ internal class NadelRenameArgumentInputTypesTransform : NadelTransform<State> {

override suspend fun getResultInstructions(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
overallField: ExecutableNormalizedField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.nadel.Service
import graphql.nadel.ServiceExecutionHydrationDetails
import graphql.nadel.ServiceExecutionResult
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.NadelOverallExecutionBlueprint
import graphql.nadel.engine.blueprint.NadelRenameFieldInstruction
import graphql.nadel.engine.blueprint.getTypeNameToInstructionMap
Expand Down Expand Up @@ -38,6 +39,7 @@ internal class NadelRenameTransform : NadelTransform<State> {

override suspend fun isApplicable(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
services: Map<String, Service>,
service: Service,
Expand Down Expand Up @@ -66,6 +68,7 @@ internal class NadelRenameTransform : NadelTransform<State> {

override suspend fun transformField(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
transformer: NadelQueryTransformer,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
Expand Down Expand Up @@ -173,10 +176,11 @@ internal class NadelRenameTransform : NadelTransform<State> {

override suspend fun getResultInstructions(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
overallField: ExecutableNormalizedField,
underlyingParentField: ExecutableNormalizedField?, // Overall field
underlyingParentField: ExecutableNormalizedField?,
result: ServiceExecutionResult,
state: State,
nodes: JsonNodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import graphql.nadel.Service
import graphql.nadel.ServiceExecutionHydrationDetails
import graphql.nadel.ServiceExecutionResult
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.IntrospectionService
import graphql.nadel.engine.blueprint.NadelOverallExecutionBlueprint
import graphql.nadel.engine.transform.NadelServiceTypeFilterTransform.State
Expand Down Expand Up @@ -71,6 +72,7 @@ class NadelServiceTypeFilterTransform : NadelTransform<State> {

override suspend fun isApplicable(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
services: Map<String, Service>,
service: Service,
Expand Down Expand Up @@ -120,6 +122,7 @@ class NadelServiceTypeFilterTransform : NadelTransform<State> {

override suspend fun transformField(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
transformer: NadelQueryTransformer,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
Expand Down Expand Up @@ -175,6 +178,7 @@ class NadelServiceTypeFilterTransform : NadelTransform<State> {

override suspend fun getResultInstructions(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
overallField: ExecutableNormalizedField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import graphql.nadel.Service
import graphql.nadel.ServiceExecutionHydrationDetails
import graphql.nadel.ServiceExecutionResult
import graphql.nadel.engine.NadelExecutionContext
import graphql.nadel.engine.NadelServiceExecutionContext
import graphql.nadel.engine.blueprint.NadelOverallExecutionBlueprint
import graphql.nadel.engine.transform.query.NadelQueryTransformer
import graphql.nadel.engine.transform.result.NadelResultInstruction
Expand Down Expand Up @@ -40,6 +41,7 @@ interface NadelTransform<State : Any> {
*/
suspend fun isApplicable(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
services: Map<String, Service>,
service: Service,
Expand All @@ -56,6 +58,7 @@ interface NadelTransform<State : Any> {
*/
suspend fun transformField(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
transformer: NadelQueryTransformer,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
Expand All @@ -71,6 +74,7 @@ interface NadelTransform<State : Any> {
*/
suspend fun getResultInstructions(
executionContext: NadelExecutionContext,
serviceExecutionContext: NadelServiceExecutionContext,
executionBlueprint: NadelOverallExecutionBlueprint,
service: Service,
overallField: ExecutableNormalizedField,
Expand Down
Loading

0 comments on commit 41d1b44

Please sign in to comment.