Skip to content

Commit

Permalink
Rever to "application/json" as default MIME type
Browse files Browse the repository at this point in the history
As of #108, the `"application/graphql+json"` media type is supported and
has been made the default.

It seems that it is not ready for adoption now; so while we'll keep
supporting it, this commit reverts back to `"application/json"` as the
default media type for servers and clients.

Closes gh-375
  • Loading branch information
bclozel committed May 4, 2022
1 parent 81a03f2 commit 85ad5bb
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ final class WebTestClientTransport implements GraphQlTransport {
public Mono<GraphQlResponse> execute(GraphQlRequest request) {

Map<String, Object> responseMap = this.webTestClient.post()
.contentType(MediaType.APPLICATION_GRAPHQL)
.accept(MediaType.APPLICATION_GRAPHQL)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(request.toMap())
.exchange()
.expectStatus().isOk()
.expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_GRAPHQL)
.expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
.expectBody(MAP_TYPE)
.returnResult()
.getResponseBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class DefaultRSocketGraphQlClientBuilder
}

private static RSocketRequester.Builder initRSocketRequestBuilder() {
RSocketRequester.Builder requesterBuilder = RSocketRequester.builder().dataMimeType(MimeTypeUtils.APPLICATION_GRAPHQL);
RSocketRequester.Builder requesterBuilder = RSocketRequester.builder().dataMimeType(MimeTypeUtils.APPLICATION_JSON);
if (jackson2Present) {
requesterBuilder.rsocketStrategies(
RSocketStrategies.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ private static MediaType initContentType(WebClient webClient) {
HttpHeaders headers = new HttpHeaders();
webClient.mutate().defaultHeaders(headers::putAll);
MediaType contentType = headers.getContentType();
return (contentType != null ? contentType : MediaType.APPLICATION_GRAPHQL);
return (contentType != null ? contentType : MediaType.APPLICATION_JSON);
}


@Override
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
return this.webClient.post()
.contentType(this.contentType)
.accept(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL)
.bodyValue(request.toMap())
.retrieve()
.bodyToMono(MAP_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static MediaType selectResponseMediaType(ServerRequest serverRequest) {
return accepted;
}
}
return MediaType.APPLICATION_GRAPHQL;
return MediaType.APPLICATION_JSON;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static MediaType selectResponseMediaType(ServerRequest serverRequest) {
return accepted;
}
}
return MediaType.APPLICATION_GRAPHQL;
return MediaType.APPLICATION_JSON;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,18 @@ void contentTypeDefault() {
setup.initBuilder().build().document(DOCUMENT).execute().block(TIMEOUT);

WebGraphQlRequest request = setup.getActualRequest();
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL);
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}

@Test
void contentTypeOverride() {

HttpBuilderSetup setup = new HttpBuilderSetup();
setup.initBuilder().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build()
setup.initBuilder().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_GRAPHQL_VALUE).build()
.document(DOCUMENT).execute().block(TIMEOUT);

WebGraphQlRequest request = setup.getActualRequest();
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public class GraphQlHttpHandlerTests {


@Test
void shouldProduceApplicationGraphQlByDefault() {
void shouldProduceApplicationJsonByDefault() {
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
.contentType(MediaType.APPLICATION_GRAPHQL).accept(MediaType.ALL).build();
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.ALL).build();

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

assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL);
assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}

@Test
Expand All @@ -75,7 +75,7 @@ void shouldProduceApplicationGraphQl() {
@Test
void shouldProduceApplicationJson() {
MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
.contentType(MediaType.APPLICATION_GRAPHQL).accept(MediaType.APPLICATION_JSON).build();
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).build();

MockServerHttpResponse httpResponse = handleRequest(
httpRequest, this.greetingHandler, Collections.singletonMap("query", "{greeting}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public class GraphQlHttpHandlerTests {
.queryFetcher("greeting", (env) -> "Hello").toHttpHandler();

@Test
void shouldProduceApplicationGraphQlByDefault() throws Exception {
void shouldProduceApplicationJsonByDefault() throws Exception {
MockHttpServletRequest servletRequest = createServletRequest("{\"query\":\"{ greeting }\"}", "*/*");
MockHttpServletResponse servletResponse = handleRequest(servletRequest, this.greetingHandler);
assertThat(servletResponse.getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_VALUE);
assertThat(servletResponse.getContentType()).isEqualTo(MediaType.APPLICATION_JSON_VALUE);
}

@Test
Expand Down Expand Up @@ -111,7 +111,7 @@ void shouldSetExecutionId() throws Exception {

private MockHttpServletRequest createServletRequest(String query, String accept) {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("POST", "/");
servletRequest.setContentType(MediaType.APPLICATION_GRAPHQL_VALUE);
servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
servletRequest.setContent(query.getBytes(StandardCharsets.UTF_8));
servletRequest.addHeader("Accept", accept);
servletRequest.setAsyncSupported(true);
Expand Down

0 comments on commit 85ad5bb

Please sign in to comment.