-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2104 from Netflix/feature/dgs-headers-backward-co…
…mpatibility Dgs headers backward compatibility
- Loading branch information
Showing
4 changed files
with
155 additions
and
166 deletions.
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
126 changes: 126 additions & 0 deletions
126
...l/src/test/kotlin/com/netflix/graphql/dgs/springgraphql/autoconfig/DgsHeadersSmokeTest.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,126 @@ | ||
/* | ||
* 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.autoconfig | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.netflix.graphql.dgs.DgsComponent | ||
import com.netflix.graphql.dgs.DgsExecutionResult | ||
import com.netflix.graphql.dgs.DgsQuery | ||
import graphql.ExecutionResult | ||
import graphql.execution.instrumentation.InstrumentationState | ||
import graphql.execution.instrumentation.SimplePerformantInstrumentation | ||
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration | ||
import org.springframework.boot.autoconfigure.graphql.servlet.GraphQlWebMvcAutoConfiguration | ||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.TestConfiguration | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Component | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.post | ||
import java.util.concurrent.CompletableFuture | ||
|
||
@SpringBootTest( | ||
classes = [ | ||
DgsHeadersSmokeTest.TestApp::class, | ||
DgsSpringGraphQLAutoConfiguration::class, | ||
GraphQlAutoConfiguration::class, | ||
GraphQlWebMvcAutoConfiguration::class, | ||
WebMvcAutoConfiguration::class, | ||
], | ||
properties = [ | ||
"dgs.graphql.schema-locations=classpath:/dgs-spring-graphql-smoke-test.graphqls", | ||
], | ||
) | ||
@AutoConfigureMockMvc | ||
class DgsHeadersSmokeTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Test | ||
fun `Response headers can be set by using DgsExecutionResult`() { | ||
val query = | ||
""" | ||
query { | ||
dgsField | ||
} | ||
""".trimIndent() | ||
|
||
data class GraphQlRequest( | ||
val query: String, | ||
) | ||
|
||
mockMvc | ||
.post("/graphql") { | ||
content = jacksonObjectMapper().writeValueAsString(GraphQlRequest(query)) | ||
accept = MediaType.APPLICATION_JSON | ||
contentType = MediaType.APPLICATION_JSON | ||
}.andExpect { | ||
status { isOk() } | ||
header { string("dgsexecutionresultheader", "set from DgsExecutionResult") } | ||
header { string("extensionheader", "set from extensions") } | ||
} | ||
} | ||
|
||
@TestConfiguration | ||
open class TestApp { | ||
@DgsComponent | ||
open class DgsTestDatafetcher { | ||
@DgsQuery | ||
fun dgsField(): String = "test from DGS" | ||
|
||
@Component | ||
open class MyIntrospection : SimplePerformantInstrumentation() { | ||
override fun instrumentExecutionResult( | ||
executionResult: ExecutionResult, | ||
parameters: InstrumentationExecutionParameters, | ||
state: InstrumentationState?, | ||
): CompletableFuture<ExecutionResult> { | ||
val headers = HttpHeaders() | ||
headers.add("dgsexecutionresultheader", "set from DgsExecutionResult") | ||
|
||
val extensionHeaders = HttpHeaders() | ||
extensionHeaders.add("extensionheader", "set from extensions") | ||
val updatedExecutionResult = | ||
executionResult.transform { r -> | ||
r.extensions( | ||
mapOf( | ||
Pair( | ||
"dgs-response-headers", | ||
extensionHeaders, | ||
), | ||
), | ||
) | ||
} | ||
|
||
return CompletableFuture.completedFuture( | ||
DgsExecutionResult | ||
.builder() | ||
.executionResult(updatedExecutionResult) | ||
.headers(headers) | ||
.build(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
93 changes: 0 additions & 93 deletions
93
graphql-dgs/src/test/kotlin/com/netflix/graphql/dgs/internal/DgsExecutionResultTest.kt
This file was deleted.
Oops, something went wrong.