|
| 1 | +/* |
| 2 | + * Copyright 2022 Expedia, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.expediagroup.graphql.transactionbatcher.instrumentation.execution |
| 18 | + |
| 19 | +import com.expediagroup.graphql.transactionbatcher.instrumentation.state.ExecutionLevelInstrumentationState |
| 20 | +import graphql.ExecutionResult |
| 21 | +import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext |
| 22 | +import graphql.execution.instrumentation.InstrumentationContext |
| 23 | +import graphql.execution.instrumentation.SimpleInstrumentation |
| 24 | +import graphql.execution.instrumentation.SimpleInstrumentationContext |
| 25 | +import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters |
| 26 | +import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters |
| 27 | +import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters |
| 28 | +import graphql.schema.DataFetcher |
| 29 | + |
| 30 | +/** |
| 31 | + * Custom GraphQL [graphql.execution.instrumentation.Instrumentation] that calculate the state of executions |
| 32 | + * of all queries sharing the same GraphQLContext map |
| 33 | + */ |
| 34 | +abstract class AbstractExecutionLevelInstrumentation : SimpleInstrumentation(), ExecutionLevelInstrumentation { |
| 35 | + |
| 36 | + override fun beginExecuteOperation( |
| 37 | + parameters: InstrumentationExecuteOperationParameters |
| 38 | + ): InstrumentationContext<ExecutionResult> = |
| 39 | + parameters.executionContext |
| 40 | + .graphQLContext.get<ExecutionLevelInstrumentationState>(ExecutionLevelInstrumentationState::class) |
| 41 | + ?.beginExecuteOperation(parameters) |
| 42 | + ?: SimpleInstrumentationContext.noOp() |
| 43 | + |
| 44 | + override fun beginExecutionStrategy( |
| 45 | + parameters: InstrumentationExecutionStrategyParameters |
| 46 | + ): ExecutionStrategyInstrumentationContext = |
| 47 | + parameters.executionContext |
| 48 | + .graphQLContext.get<ExecutionLevelInstrumentationState>(ExecutionLevelInstrumentationState::class) |
| 49 | + ?.beginExecutionStrategy( |
| 50 | + parameters, |
| 51 | + this.calculateLevelState( |
| 52 | + ExecutionLevelInstrumentationParameters( |
| 53 | + parameters.executionContext, |
| 54 | + ExecutionLevelCalculationSource.EXECUTION_STRATEGY |
| 55 | + ) |
| 56 | + ) |
| 57 | + ) |
| 58 | + ?: NoOpExecutionStrategyInstrumentationContext |
| 59 | + |
| 60 | + override fun beginFieldFetch( |
| 61 | + parameters: InstrumentationFieldFetchParameters |
| 62 | + ): InstrumentationContext<Any> = |
| 63 | + parameters.executionContext |
| 64 | + .graphQLContext.get<ExecutionLevelInstrumentationState>(ExecutionLevelInstrumentationState::class) |
| 65 | + ?.beginFieldFetch( |
| 66 | + parameters, |
| 67 | + this.calculateLevelState( |
| 68 | + ExecutionLevelInstrumentationParameters( |
| 69 | + parameters.executionContext, |
| 70 | + ExecutionLevelCalculationSource.FIELD_FETCH |
| 71 | + ) |
| 72 | + ) |
| 73 | + ) |
| 74 | + ?: SimpleInstrumentationContext.noOp() |
| 75 | + |
| 76 | + override fun instrumentDataFetcher( |
| 77 | + dataFetcher: DataFetcher<*>, |
| 78 | + parameters: InstrumentationFieldFetchParameters |
| 79 | + ): DataFetcher<*> = |
| 80 | + parameters.executionContext |
| 81 | + .graphQLContext.get<ExecutionLevelInstrumentationState>(ExecutionLevelInstrumentationState::class) |
| 82 | + ?.instrumentDataFetcher(dataFetcher, parameters) |
| 83 | + ?: dataFetcher |
| 84 | +} |
0 commit comments