Add GraphQL response media type
This commit introduces a static `MediaType` instance for `"application/graphql-response+json"`. Closes gh-1110
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2020-2025 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.graphql;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* Constants for well-known GraphQL media types.
|
||||
* @author Brian Clozel
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public abstract class MediaTypes {
|
||||
|
||||
/**
|
||||
* Standard media type for GraphQL responses over the HTTP protocol.
|
||||
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">
|
||||
* GraphQL over HTTP specification</a>
|
||||
*/
|
||||
public static final MediaType APPLICATION_GRAPHQL_RESPONSE =
|
||||
MediaType.parseMediaType("application/graphql-response+json");
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.graphql.GraphQlRequest;
|
||||
import org.springframework.graphql.GraphQlResponse;
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.ServerSentEvent;
|
||||
@@ -75,7 +76,7 @@ final class HttpGraphQlTransport implements GraphQlTransport {
|
||||
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
|
||||
return this.webClient.post()
|
||||
.contentType(this.contentType)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE, APPLICATION_GRAPHQL)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE, APPLICATION_GRAPHQL)
|
||||
.bodyValue(request.toMap())
|
||||
.attributes((attributes) -> {
|
||||
if (request instanceof ClientGraphQlRequest clientRequest) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.Map;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.graphql.GraphQlRequest;
|
||||
import org.springframework.graphql.GraphQlResponse;
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -62,7 +63,7 @@ final class HttpSyncGraphQlTransport implements SyncGraphQlTransport {
|
||||
|
||||
Map<String, Object> body = this.restClient.post()
|
||||
.contentType(this.contentType)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.body(request.toMap())
|
||||
.retrieve()
|
||||
.body(MAP_TYPE);
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.graphql.server.WebGraphQlHandler;
|
||||
import org.springframework.graphql.server.WebGraphQlResponse;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -40,7 +41,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
|
||||
new MediaType("application", "graphql+json");
|
||||
|
||||
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
|
||||
MediaType.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
|
||||
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -58,7 +59,7 @@ public final class GraphQlRequestPredicates {
|
||||
*/
|
||||
public static RequestPredicate graphQlHttp(String path) {
|
||||
return new GraphQlHttpRequestPredicate(
|
||||
path, List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE));
|
||||
path, List.of(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.concurrent.ExecutionException;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.graphql.server.WebGraphQlHandler;
|
||||
import org.springframework.graphql.server.WebGraphQlResponse;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -45,7 +46,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
|
||||
new MediaType("application", "graphql+json");
|
||||
|
||||
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
|
||||
MediaType.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
|
||||
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2024 the original author or authors.
|
||||
* Copyright 2020-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -58,7 +59,7 @@ public final class GraphQlRequestPredicates {
|
||||
*/
|
||||
public static RequestPredicate graphQlHttp(String path) {
|
||||
return new GraphQlHttpRequestPredicate(
|
||||
path, List.of(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE));
|
||||
path, List.of(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.core.codec.DataBufferEncoder;
|
||||
import org.springframework.core.io.buffer.DefaultDataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.graphql.server.WebGraphQlHandler;
|
||||
import org.springframework.graphql.server.support.SerializableGraphQlRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -119,12 +120,12 @@ public class GraphQlHttpHandlerTests {
|
||||
void shouldProduceApplicationGraphQl() throws Exception {
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.body(initRequestBody("{greeting}"));
|
||||
|
||||
MockServerHttpResponse httpResponse = handleRequest(httpRequest, this.greetingHandler);
|
||||
|
||||
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE);
|
||||
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaTypes.APPLICATION_GRAPHQL_RESPONSE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,7 +148,7 @@ public class GraphQlHttpHandlerTests {
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.acceptLanguageAsLocales(Locale.FRENCH)
|
||||
.body(initRequestBody("{greeting}"));
|
||||
|
||||
@@ -165,7 +166,7 @@ public class GraphQlHttpHandlerTests {
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.body(initRequestBody("{showId}"));
|
||||
|
||||
MockServerHttpResponse httpResponse = handleRequest(httpRequest, handler);
|
||||
@@ -191,7 +192,7 @@ public class GraphQlHttpHandlerTests {
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.accept(MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.body(body);
|
||||
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(httpRequest);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2024 the original author or authors.
|
||||
* Copyright 2020-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import java.util.Collections;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -131,7 +132,7 @@ class GraphQlRequestPredicatesTests {
|
||||
private MockServerWebExchange createMatchingHttpExchange() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/graphql")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE)
|
||||
.build();
|
||||
return MockServerWebExchange.from(request);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.MediaTypes;
|
||||
import org.springframework.graphql.server.WebGraphQlHandler;
|
||||
import org.springframework.graphql.server.support.SerializableGraphQlRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -91,9 +92,9 @@ public class GraphQlHttpHandlerTests {
|
||||
|
||||
@Test
|
||||
void shouldProduceApplicationGraphQl() throws Exception {
|
||||
MockHttpServletRequest request = createServletRequest("{ greeting }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
|
||||
MockHttpServletRequest request = createServletRequest("{ greeting }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
|
||||
MockHttpServletResponse response = handleRequest(request, this.greetingHandler);
|
||||
assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
|
||||
assertThat(response.getContentType()).isEqualTo(MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,7 +111,7 @@ public class GraphQlHttpHandlerTests {
|
||||
.toHttpHandler();
|
||||
|
||||
MockHttpServletRequest request = createServletRequest(
|
||||
"{ greeting }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
|
||||
"{ greeting }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
|
||||
|
||||
LocaleContextHolder.setLocale(Locale.FRENCH);
|
||||
|
||||
@@ -130,7 +131,7 @@ public class GraphQlHttpHandlerTests {
|
||||
.toHttpHandler();
|
||||
|
||||
MockHttpServletRequest request = createServletRequest(
|
||||
"{ showId }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
|
||||
"{ showId }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
|
||||
|
||||
MockHttpServletResponse response = handleRequest(request, handler);
|
||||
|
||||
@@ -145,7 +146,7 @@ public class GraphQlHttpHandlerTests {
|
||||
.queryFetcher("greeting", (env) -> "Hello").toWebGraphQlHandler();
|
||||
|
||||
GraphQlHttpHandler handler = new GraphQlHttpHandler(webGraphQlHandler, new MappingJackson2HttpMessageConverter());
|
||||
MockHttpServletRequest servletRequest = createServletRequest("{ greeting }", MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE);
|
||||
MockHttpServletRequest servletRequest = createServletRequest("{ greeting }", MediaTypes.APPLICATION_GRAPHQL_RESPONSE.toString());
|
||||
|
||||
ServerRequest request = ServerRequest.create(servletRequest, Collections.emptyList());
|
||||
ServerResponse response = handler.handleRequest(request);
|
||||
|
||||
Reference in New Issue
Block a user