|
| 1 | +package com.expediagroup.graphql.spring.instrumentation |
| 2 | + |
| 3 | +import com.expediagroup.graphql.spring.DEFAULT_INSTRUMENTATION_ORDER |
| 4 | +import com.expediagroup.graphql.spring.model.GraphQLRequest |
| 5 | +import com.expediagroup.graphql.spring.operations.Query |
| 6 | +import graphql.ExecutionResult |
| 7 | +import graphql.ExecutionResultImpl |
| 8 | +import graphql.execution.instrumentation.Instrumentation |
| 9 | +import graphql.execution.instrumentation.SimpleInstrumentation |
| 10 | +import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters |
| 11 | +import org.junit.jupiter.api.Test |
| 12 | +import org.springframework.beans.factory.annotation.Autowired |
| 13 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration |
| 14 | +import org.springframework.boot.test.context.SpringBootTest |
| 15 | +import org.springframework.context.annotation.Bean |
| 16 | +import org.springframework.context.annotation.Configuration |
| 17 | +import org.springframework.core.Ordered |
| 18 | +import org.springframework.http.MediaType |
| 19 | +import org.springframework.test.web.reactive.server.WebTestClient |
| 20 | +import java.util.concurrent.CompletableFuture |
| 21 | +import java.util.concurrent.atomic.AtomicInteger |
| 22 | + |
| 23 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = ["graphql.packages=com.expediagroup.graphql.spring.instrumentation"]) |
| 24 | +@EnableAutoConfiguration |
| 25 | +class InstrumentationIT(@Autowired private val testClient: WebTestClient) { |
| 26 | + |
| 27 | + @Configuration |
| 28 | + class TestConfiguration { |
| 29 | + private val atomicCounter = AtomicInteger() |
| 30 | + |
| 31 | + @Bean |
| 32 | + fun query(): Query = BasicQuery() |
| 33 | + |
| 34 | + @Bean |
| 35 | + fun firstInstrumentation(): Instrumentation = OrderedInstrumentation(DEFAULT_INSTRUMENTATION_ORDER, atomicCounter) |
| 36 | + |
| 37 | + @Bean |
| 38 | + fun secondInstrumentation(): Instrumentation = OrderedInstrumentation(DEFAULT_INSTRUMENTATION_ORDER + 1, atomicCounter) |
| 39 | + } |
| 40 | + |
| 41 | + class BasicQuery : Query { |
| 42 | + fun helloWorld(name: String) = "Hello $name!" |
| 43 | + } |
| 44 | + |
| 45 | + class OrderedInstrumentation(private val instrumentationOrder: Int, private val counter: AtomicInteger) : SimpleInstrumentation(), Ordered { |
| 46 | + override fun instrumentExecutionResult(executionResult: ExecutionResult, parameters: InstrumentationExecutionParameters): CompletableFuture<ExecutionResult> { |
| 47 | + val extensions = mutableMapOf<Any, Any>() |
| 48 | + extensions[instrumentationOrder] = counter.getAndIncrement() |
| 49 | + val currentExt: Map<Any, Any>? = executionResult.extensions |
| 50 | + if (currentExt != null) { |
| 51 | + extensions.putAll(currentExt) |
| 52 | + } |
| 53 | + return CompletableFuture.completedFuture(ExecutionResultImpl(executionResult.getData(), executionResult.errors, extensions)) |
| 54 | + } |
| 55 | + |
| 56 | + override fun getOrder(): Int = instrumentationOrder |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `verify instrumentations are applied in the specified order`() { |
| 61 | + testClient.post() |
| 62 | + .uri("/graphql") |
| 63 | + .accept(MediaType.APPLICATION_JSON) |
| 64 | + .contentType(MediaType.APPLICATION_JSON) |
| 65 | + .bodyValue(GraphQLRequest("query { helloWorld(name: \"World\") }")) |
| 66 | + .exchange() |
| 67 | + .expectBody() |
| 68 | + .jsonPath("$.data.helloWorld").isEqualTo("Hello World!") |
| 69 | + .jsonPath("$.errors").doesNotExist() |
| 70 | + .jsonPath("$.extensions").exists() |
| 71 | + .jsonPath("$.extensions.0").isEqualTo(0) |
| 72 | + .jsonPath("$.extensions.1").isEqualTo(1) |
| 73 | + } |
| 74 | +} |
0 commit comments