Skip to content

Commit 573e899

Browse files
committed
Adapt to GraphQL over HTTP spec changes
As of graphql/graphql-over-http#215, the official media type for GraphQL HTTP responses is now `"application/graphql-response+json"` instead of `"application/graphql+json"`. The latter is now deprecated and support will be removed in the future. This commit now favors the new media type. HTTP Clients are still supposed to send requests with the `"application/json"` content type. Closes gh-563
1 parent eb9b1f3 commit 573e899

File tree

6 files changed

+30
-13
lines changed

6 files changed

+30
-13
lines changed

spring-graphql/src/main/java/org/springframework/graphql/client/HttpGraphQlTransport.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ final class HttpGraphQlTransport implements GraphQlTransport {
4444
private static final ParameterizedTypeReference<Map<String, Object>> MAP_TYPE =
4545
new ParameterizedTypeReference<Map<String, Object>>() {};
4646

47+
// To be removed in favor of Framework's MediaType.APPLICATION_GRAPHQL_RESPONSE
48+
private static final MediaType APPLICATION_GRAPHQL_RESPONSE =
49+
new MediaType("application", "graphql-response+json");
50+
4751

4852
private final WebClient webClient;
4953

@@ -65,10 +69,11 @@ private static MediaType initContentType(WebClient webClient) {
6569

6670

6771
@Override
72+
@SuppressWarnings("removal")
6873
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
6974
return this.webClient.post()
7075
.contentType(this.contentType)
71-
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL)
76+
.accept(MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_GRAPHQL)
7277
.bodyValue(request.toMap())
7378
.retrieve()
7479
.bodyToMono(MAP_TYPE)

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/GraphQlHttpHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ public class GraphQlHttpHandler {
4343

4444
private static final Log logger = LogFactory.getLog(GraphQlHttpHandler.class);
4545

46+
// To be removed in favor of Framework's MediaType.APPLICATION_GRAPHQL_RESPONSE
47+
private static final MediaType APPLICATION_GRAPHQL_RESPONSE =
48+
new MediaType("application", "graphql-response+json");
49+
4650
private static final ParameterizedTypeReference<Map<String, Object>> MAP_PARAMETERIZED_TYPE_REF =
4751
new ParameterizedTypeReference<Map<String, Object>>() {};
4852

53+
@SuppressWarnings("removal")
4954
private static final List<MediaType> SUPPORTED_MEDIA_TYPES =
50-
Arrays.asList(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON);
55+
Arrays.asList(APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL);
5156

5257
private final WebGraphQlHandler graphQlHandler;
5358

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ public class GraphQlHttpHandler {
5555
private static final ParameterizedTypeReference<Map<String, Object>> MAP_PARAMETERIZED_TYPE_REF =
5656
new ParameterizedTypeReference<Map<String, Object>>() {};
5757

58+
// To be removed in favor of Framework's MediaType.APPLICATION_GRAPHQL_RESPONSE
59+
private static final MediaType APPLICATION_GRAPHQL_RESPONSE =
60+
new MediaType("application", "graphql-response+json");
61+
62+
@SuppressWarnings("removal")
5863
private static final List<MediaType> SUPPORTED_MEDIA_TYPES =
59-
Arrays.asList(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON);
64+
Arrays.asList(APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL);
6065

6166
private final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
6267

spring-graphql/src/test/java/org/springframework/graphql/client/WebGraphQlClientBuilderTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,14 @@ void contentTypeDefault() {
189189

190190
@Test
191191
void contentTypeOverride() {
192+
MediaType testMediaType = new MediaType("application", "graphql-request+json");
192193

193194
HttpBuilderSetup setup = new HttpBuilderSetup();
194-
setup.initBuilder().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_GRAPHQL_VALUE).build()
195+
setup.initBuilder().header(HttpHeaders.CONTENT_TYPE, "application/graphql-request+json").build()
195196
.document(DOCUMENT).execute().block(TIMEOUT);
196197

197198
WebGraphQlRequest request = setup.getActualRequest();
198-
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL);
199+
assertThat(request.getHeaders().getContentType()).isEqualTo(testMediaType);
199200

200201
}
201202

spring-graphql/src/test/java/org/springframework/graphql/server/webflux/GraphQlHttpHandlerTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ void shouldProduceApplicationJsonByDefault() {
6464
@Test
6565
void shouldProduceApplicationGraphQl() {
6666
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
67-
.contentType(MediaType.APPLICATION_GRAPHQL).accept(MediaType.APPLICATION_GRAPHQL).build();
67+
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_GRAPHQL_RESPONSE).build();
6868

6969
MockServerHttpResponse httpResponse = handleRequest(
7070
httpRequest, this.greetingHandler, Collections.singletonMap("query", "{greeting}"));
7171

72-
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL);
72+
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE);
7373
}
7474

7575
@Test
@@ -90,7 +90,8 @@ void locale() {
9090
.toHttpHandlerWebFlux();
9191

9292
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
93-
.contentType(MediaType.APPLICATION_GRAPHQL).accept(MediaType.APPLICATION_GRAPHQL).acceptLanguageAsLocales(Locale.FRENCH).build();
93+
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
94+
.acceptLanguageAsLocales(Locale.FRENCH).build();
9495

9596
MockServerHttpResponse httpResponse = handleRequest(
9697
httpRequest, handler, Collections.singletonMap("query", "{greeting}"));
@@ -106,7 +107,7 @@ void shouldSetExecutionId() {
106107
.toHttpHandlerWebFlux();
107108

108109
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
109-
.contentType(MediaType.APPLICATION_GRAPHQL).accept(MediaType.APPLICATION_GRAPHQL).build();
110+
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_GRAPHQL_RESPONSE).build();
110111

111112
MockServerHttpResponse httpResponse = handleRequest(
112113
httpRequest, handler, Collections.singletonMap("query", "{showId}"));

spring-graphql/src/test/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandlerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ void shouldProduceApplicationJsonByDefault() throws Exception {
6464

6565
@Test
6666
void shouldProduceApplicationGraphQl() throws Exception {
67-
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ greeting }\"}", MediaType.APPLICATION_GRAPHQL_VALUE);
67+
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ greeting }\"}", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
6868
MockHttpServletResponse servletResponse = handleRequest(servletRequest, this.greetingHandler);
69-
assertThat(servletResponse.getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_VALUE);
69+
assertThat(servletResponse.getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
7070
}
7171

7272
@Test
@@ -81,7 +81,7 @@ void locale() throws Exception {
8181
GraphQlHttpHandler handler = GraphQlSetup.schemaContent("type Query { greeting: String }")
8282
.queryFetcher("greeting", (env) -> "Hello in " + env.getLocale())
8383
.toHttpHandler();
84-
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ greeting }\"}", MediaType.APPLICATION_GRAPHQL_VALUE);
84+
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ greeting }\"}", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
8585
LocaleContextHolder.setLocale(Locale.FRENCH);
8686

8787
try {
@@ -101,7 +101,7 @@ void shouldSetExecutionId() throws Exception {
101101
.queryFetcher("showId", (env) -> env.getExecutionId().toString())
102102
.toHttpHandler();
103103

104-
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ showId }\"}", MediaType.APPLICATION_GRAPHQL_VALUE);
104+
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ showId }\"}", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
105105

106106
MockHttpServletResponse servletResponse = handleRequest(servletRequest, handler);
107107
DocumentContext document = JsonPath.parse(servletResponse.getContentAsString());

0 commit comments

Comments
 (0)