Skip to content

Commit c015dbc

Browse files
committed
Add GraphQL response media type
This commit introduces a static `MediaType` instance for `"application/graphql-response+json"`. Closes gh-1110
1 parent c1938fc commit c015dbc

File tree

10 files changed

+64
-19
lines changed

10 files changed

+64
-19
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2020-2025 the original author or authors.
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 org.springframework.graphql;
18+
19+
import org.springframework.http.MediaType;
20+
21+
/**
22+
* Constants for well-known GraphQL media types.
23+
* @author Brian Clozel
24+
* @since 1.4.0
25+
*/
26+
public abstract class MediaTypes {
27+
28+
/**
29+
* Standard media type for GraphQL responses over the HTTP protocol.
30+
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">
31+
* GraphQL over HTTP specification</a>
32+
*/
33+
public static final MediaType APPLICATION_GRAPHQL_RESPONSE =
34+
MediaType.parseMediaType("application/graphql-response+json");
35+
36+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.core.ParameterizedTypeReference;
2525
import org.springframework.graphql.GraphQlRequest;
2626
import org.springframework.graphql.GraphQlResponse;
27+
import org.springframework.graphql.MediaTypes;
2728
import org.springframework.http.HttpHeaders;
2829
import org.springframework.http.MediaType;
2930
import org.springframework.http.codec.ServerSentEvent;
@@ -75,7 +76,7 @@ private static MediaType initContentType(WebClient webClient) {
7576
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
7677
return this.webClient.post()
7778
.contentType(this.contentType)
78-
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE, APPLICATION_GRAPHQL)
79+
.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE, APPLICATION_GRAPHQL)
7980
.bodyValue(request.toMap())
8081
.attributes((attributes) -> {
8182
if (request instanceof ClientGraphQlRequest clientRequest) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.core.ParameterizedTypeReference;
2323
import org.springframework.graphql.GraphQlRequest;
2424
import org.springframework.graphql.GraphQlResponse;
25+
import org.springframework.graphql.MediaTypes;
2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.http.MediaType;
2728
import org.springframework.util.Assert;
@@ -62,7 +63,7 @@ public GraphQlResponse execute(GraphQlRequest request) {
6263

6364
Map<String, Object> body = this.restClient.post()
6465
.contentType(this.contentType)
65-
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE)
66+
.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
6667
.body(request.toMap())
6768
.retrieve()
6869
.body(MAP_TYPE);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import reactor.core.publisher.Mono;
2222

23+
import org.springframework.graphql.MediaTypes;
2324
import org.springframework.graphql.server.WebGraphQlHandler;
2425
import org.springframework.graphql.server.WebGraphQlResponse;
2526
import org.springframework.http.MediaType;
@@ -40,7 +41,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
4041
new MediaType("application", "graphql+json");
4142

4243
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
43-
MediaType.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
44+
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
4445

4546

4647
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
2424

25+
import org.springframework.graphql.MediaTypes;
2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.MediaType;
@@ -58,7 +59,7 @@ private GraphQlRequestPredicates() {
5859
*/
5960
public static RequestPredicate graphQlHttp(String path) {
6061
return new GraphQlHttpRequestPredicate(
61-
path, List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE));
62+
path, List.of(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE));
6263
}
6364

6465
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import reactor.core.publisher.Mono;
2525

26+
import org.springframework.graphql.MediaTypes;
2627
import org.springframework.graphql.server.WebGraphQlHandler;
2728
import org.springframework.graphql.server.WebGraphQlResponse;
2829
import org.springframework.http.MediaType;
@@ -45,7 +46,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
4546
new MediaType("application", "graphql+json");
4647

4748
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
48-
MediaType.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
49+
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
4950

5051

5152
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
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.
@@ -22,6 +22,7 @@
2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
2424

25+
import org.springframework.graphql.MediaTypes;
2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.MediaType;
@@ -58,7 +59,7 @@ private GraphQlRequestPredicates() {
5859
*/
5960
public static RequestPredicate graphQlHttp(String path) {
6061
return new GraphQlHttpRequestPredicate(
61-
path, List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE));
62+
path, List.of(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE));
6263
}
6364

6465
/**

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.core.io.buffer.DefaultDataBuffer;
3333
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3434
import org.springframework.graphql.GraphQlSetup;
35+
import org.springframework.graphql.MediaTypes;
3536
import org.springframework.graphql.server.WebGraphQlHandler;
3637
import org.springframework.graphql.server.support.SerializableGraphQlRequest;
3738
import org.springframework.http.MediaType;
@@ -119,12 +120,12 @@ void shouldSupportApplicationGraphQlWithCharset() throws Exception {
119120
void shouldProduceApplicationGraphQl() throws Exception {
120121
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
121122
.contentType(MediaType.APPLICATION_JSON)
122-
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
123+
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
123124
.body(initRequestBody("{greeting}"));
124125

125126
MockServerHttpResponse httpResponse = handleRequest(httpRequest, this.greetingHandler);
126127

127-
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE);
128+
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaTypes.APPLICATION_GRAPHQL_RESPONSE);
128129
}
129130

130131
@Test
@@ -147,7 +148,7 @@ void locale() throws Exception {
147148

148149
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
149150
.contentType(MediaType.APPLICATION_JSON)
150-
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
151+
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
151152
.acceptLanguageAsLocales(Locale.FRENCH)
152153
.body(initRequestBody("{greeting}"));
153154

@@ -165,7 +166,7 @@ void shouldSetExecutionId() throws Exception {
165166

166167
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
167168
.contentType(MediaType.APPLICATION_JSON)
168-
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
169+
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
169170
.body(initRequestBody("{showId}"));
170171

171172
MockServerHttpResponse httpResponse = handleRequest(httpRequest, handler);
@@ -191,7 +192,7 @@ void shouldUseCustomCodec() {
191192

192193
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
193194
.contentType(MediaType.APPLICATION_JSON)
194-
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
195+
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
195196
.body(body);
196197

197198
MockServerWebExchange exchange = MockServerWebExchange.from(httpRequest);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
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.
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Nested;
2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.graphql.MediaTypes;
2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.MediaType;
@@ -131,7 +132,7 @@ void shouldNotSetAttributeWhenNoMatch() {
131132
private MockServerWebExchange createMatchingHttpExchange() {
132133
MockServerHttpRequest request = MockServerHttpRequest.post("/graphql")
133134
.contentType(MediaType.APPLICATION_JSON)
134-
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE)
135+
.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
135136
.build();
136137
return MockServerWebExchange.from(request);
137138
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.springframework.context.i18n.LocaleContextHolder;
3636
import org.springframework.graphql.GraphQlSetup;
37+
import org.springframework.graphql.MediaTypes;
3738
import org.springframework.graphql.server.WebGraphQlHandler;
3839
import org.springframework.graphql.server.support.SerializableGraphQlRequest;
3940
import org.springframework.http.MediaType;
@@ -91,9 +92,9 @@ void shouldSupportApplicationGraphQlWithCharset() throws Exception {
9192

9293
@Test
9394
void shouldProduceApplicationGraphQl() throws Exception {
94-
MockHttpServletRequest request = createServletRequest("{ greeting }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
95+
MockHttpServletRequest request = createServletRequest("{ greeting }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
9596
MockHttpServletResponse response = handleRequest(request, this.greetingHandler);
96-
assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
97+
assertThat(response.getContentType()).isEqualTo(MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
9798
}
9899

99100
@Test
@@ -110,7 +111,7 @@ void locale() throws Exception {
110111
.toHttpHandler();
111112

112113
MockHttpServletRequest request = createServletRequest(
113-
"{ greeting }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
114+
"{ greeting }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
114115

115116
LocaleContextHolder.setLocale(Locale.FRENCH);
116117

@@ -130,7 +131,7 @@ void shouldSetExecutionId() throws Exception {
130131
.toHttpHandler();
131132

132133
MockHttpServletRequest request = createServletRequest(
133-
"{ showId }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
134+
"{ showId }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
134135

135136
MockHttpServletResponse response = handleRequest(request, handler);
136137

@@ -145,7 +146,7 @@ void shouldUseCustomMessageConverter() throws Exception {
145146
.queryFetcher("greeting", (env) -> "Hello").toWebGraphQlHandler();
146147

147148
GraphQlHttpHandler handler = new GraphQlHttpHandler(webGraphQlHandler, new MappingJackson2HttpMessageConverter());
148-
MockHttpServletRequest servletRequest = createServletRequest("{ greeting }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
149+
MockHttpServletRequest servletRequest = createServletRequest("{ greeting }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
149150

150151
ServerRequest request = ServerRequest.create(servletRequest, Collections.emptyList());
151152
ServerResponse response = handler.handleRequest(request);

0 commit comments

Comments
 (0)