Skip to content

Commit 207b762

Browse files
authored
add support for application/graphql-response+json content type (#1699)
### 📝 Description Based on the accept header, spring server respond either with `application/json` (default) or `application/graphql-response+json` media type. ### 🔗 Related Issues #1573
1 parent 13df8e4 commit 207b762

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

servers/graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/server/spring/GraphQLRoutesConfiguration.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Expedia, Inc
2+
* Copyright 2023 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,11 @@ import com.expediagroup.graphql.server.spring.execution.SpringGraphQLServer
2020
import org.springframework.context.annotation.Bean
2121
import org.springframework.context.annotation.Configuration
2222
import org.springframework.context.annotation.Import
23+
import org.springframework.http.MediaType
2324
import org.springframework.web.reactive.function.server.ServerRequest
2425
import org.springframework.web.reactive.function.server.bodyValueAndAwait
2526
import org.springframework.web.reactive.function.server.buildAndAwait
2627
import org.springframework.web.reactive.function.server.coRouter
27-
import org.springframework.web.reactive.function.server.json
2828

2929
/**
3030
* Default route configuration for GraphQL endpoints.
@@ -45,8 +45,14 @@ class GraphQLRoutesConfiguration(
4545

4646
(isEndpointRequest and isNotWebSocketRequest).invoke { serverRequest ->
4747
val graphQLResponse = graphQLServer.execute(serverRequest)
48+
val acceptMediaType = serverRequest
49+
.headers()
50+
.accept()
51+
.find { it.includes(MediaType.APPLICATION_GRAPHQL_RESPONSE) }
52+
?.let { MediaType.APPLICATION_GRAPHQL_RESPONSE }
53+
?: MediaType.APPLICATION_JSON
4854
if (graphQLResponse != null) {
49-
ok().json().bodyValueAndAwait(graphQLResponse)
55+
ok().contentType(acceptMediaType).bodyValueAndAwait(graphQLResponse)
5056
} else {
5157
badRequest().buildAndAwait()
5258
}

servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/routes/RouteConfigurationIT.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2023 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -192,6 +192,19 @@ class RouteConfigurationIT(@Autowired private val testClient: WebTestClient) {
192192
.verifyGraphQLRoute("Hello POST application/graphql!")
193193
}
194194

195+
@Test
196+
fun `verify POST graphQL request with application graphql response content type`() {
197+
testClient.post()
198+
.uri("/graphql")
199+
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
200+
.contentType(graphQLMediaType)
201+
.bodyValue("query { hello(name: \"POST application/graphql\") }")
202+
.exchange()
203+
.expectHeader()
204+
.contentType(MediaType.APPLICATION_GRAPHQL_RESPONSE)
205+
.verifyGraphQLRoute("Hello POST application/graphql!")
206+
}
207+
195208
@Test
196209
fun `verify POST request with XML content type will result in HTTP 400 bad request`() {
197210
testClient.post()

0 commit comments

Comments
 (0)