Skip to content

Commit

Permalink
Support "application/graphql+json" media type in GraphQL HTTP mapping
Browse files Browse the repository at this point in the history
As seen in spring-projects/spring-graphql#108, the GraphQL HTTP spec now
requires the "application/graphql+json" media type and accepts
"application/json" for backwards compatibility.

This commit updates the `RouterFunction` definition for the GraphQL HTTP
endpoints so that both types are accepted.

Closes gh-30407
  • Loading branch information
bclozel committed Apr 4, 2022
1 parent d476d8e commit 1c71567
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 16 deletions.
Expand Up @@ -81,8 +81,8 @@
@EnableConfigurationProperties(GraphQlCorsProperties.class)
public class GraphQlWebFluxAutoConfiguration {

private static final RequestPredicate ACCEPT_JSON_CONTENT = accept(MediaType.APPLICATION_JSON)
.and(contentType(MediaType.APPLICATION_JSON));
private static final RequestPredicate SUPPORTS_MEDIATYPES = accept(MediaType.APPLICATION_GRAPHQL,
MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON));

private static final Log logger = LogFactory.getLog(GraphQlWebFluxAutoConfiguration.class);

Expand All @@ -107,7 +107,7 @@ public RouterFunction<ServerResponse> graphQlEndpoint(GraphQlHttpHandler httpHan
logger.info(LogMessage.format("GraphQL endpoint HTTP POST %s", path));
RouterFunctions.Builder builder = RouterFunctions.route();
builder = builder.GET(path, this::onlyAllowPost);
builder = builder.POST(path, ACCEPT_JSON_CONTENT, httpHandler::handleRequest);
builder = builder.POST(path, SUPPORTS_MEDIATYPES, httpHandler::handleRequest);
if (properties.getGraphiql().isEnabled()) {
GraphiQlHandler graphQlHandler = new GraphiQlHandler(path, properties.getWebsocket().getPath());
builder = builder.GET(properties.getGraphiql().getPath(), graphQlHandler::handleRequest);
Expand Down
Expand Up @@ -87,7 +87,7 @@ public class GraphQlWebMvcAutoConfiguration {

private static final Log logger = LogFactory.getLog(GraphQlWebMvcAutoConfiguration.class);

private static MediaType[] SUPPORTED_MEDIA_TYPES = new MediaType[] { MediaType.valueOf("application/graphql+json"),
private static MediaType[] SUPPORTED_MEDIA_TYPES = new MediaType[] { MediaType.APPLICATION_GRAPHQL,
MediaType.APPLICATION_JSON };

@Bean
Expand All @@ -113,8 +113,8 @@ public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler h
logger.info(LogMessage.format("GraphQL endpoint HTTP POST %s", path));
RouterFunctions.Builder builder = RouterFunctions.route();
builder = builder.GET(path, this::onlyAllowPost);
builder = builder.POST(path, RequestPredicates.contentType(MediaType.APPLICATION_JSON)
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), httpHandler::handleRequest);
builder = builder.POST(path, RequestPredicates.contentType(SUPPORTED_MEDIA_TYPES)
.and(RequestPredicates.accept(SUPPORTED_MEDIA_TYPES)), httpHandler::handleRequest);
if (properties.getGraphiql().isEnabled()) {
GraphiQlHandler graphiQLHandler = new GraphiQlHandler(path, properties.getWebsocket().getPath());
builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::handleRequest);
Expand Down
Expand Up @@ -75,7 +75,8 @@ void simpleQueryShouldWork() {
testWithWebClient((client) -> {
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }";
client.post().uri("/graphql").bodyValue("{ \"query\": \"" + query + "\"}").exchange().expectStatus().isOk()
.expectBody().jsonPath("data.bookById.name").isEqualTo("GraphQL for beginners");
.expectHeader().contentType("application/graphql+json").expectBody().jsonPath("data.bookById.name")
.isEqualTo("GraphQL for beginners");
});
}

Expand Down Expand Up @@ -150,8 +151,8 @@ private void testWithWebClient(Consumer<WebTestClient> consumer) {
this.contextRunner.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context).configureClient()
.defaultHeaders((headers) -> {
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_GRAPHQL);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_GRAPHQL));
}).baseUrl(BASE_URL).build();
consumer.accept(client);
});
Expand Down
Expand Up @@ -81,7 +81,7 @@ void simpleQueryShouldWork() {
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }";
MvcResult result = mockMvc.perform(post("/graphql").content("{\"query\": \"" + query + "\"}")).andReturn();
mockMvc.perform(asyncDispatch(result)).andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_GRAPHQL))
.andExpect(jsonPath("data.bookById.name").value("GraphQL for beginners"));
});
}
Expand Down Expand Up @@ -155,9 +155,9 @@ void shouldConfigureWebSocketBeans() {

private void testWith(MockMvcConsumer mockMvcConsumer) {
this.contextRunner.run((context) -> {
MediaType mediaType = MediaType.APPLICATION_JSON;
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context)
.defaultRequest(post("/graphql").contentType(mediaType).accept(mediaType)).build();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).defaultRequest(
post("/graphql").contentType(MediaType.APPLICATION_GRAPHQL).accept(MediaType.APPLICATION_GRAPHQL))
.build();
mockMvcConsumer.accept(mockMvc);
});
}
Expand Down
Expand Up @@ -79,7 +79,7 @@ static class TestHandler implements HttpHandler {
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
response.setStatusCode(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
response.getHeaders().setContentType(MediaType.APPLICATION_GRAPHQL);
return response.writeWith(Mono.just(factory.wrap("{\"data\":{}}".getBytes())));
}

Expand Down
Expand Up @@ -79,7 +79,7 @@ static class TestHandler implements HttpHandler {
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
response.setStatusCode(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
response.getHeaders().setContentType(MediaType.APPLICATION_GRAPHQL);
return response.writeWith(Mono.just(factory.wrap("{\"data\":{}}".getBytes())));
}

Expand Down
Expand Up @@ -70,7 +70,7 @@ DispatcherServlet dispatcherServlet() {
@RestController
static class TestController {

@PostMapping(path = "/graphql", produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(path = "/graphql", produces = MediaType.APPLICATION_GRAPHQL_VALUE)
String graphql() {
return "{}";
}
Expand Down

0 comments on commit 1c71567

Please sign in to comment.