Skip to content

Commit

Permalink
Update config flag for legacy HTTP behavior
Browse files Browse the repository at this point in the history
This commit renames and changes the behavior of the configuration option
on `GraphQlHttpHandler` implementations. The
`setHttpOkOnValidationErrors` option is `false` by default and is
introduced as a deprecated method right away. Our goal here is to fade
out this option as soon as possible the traditional behavior for
"application/graphql-response+json" media types.

See gh-1117
  • Loading branch information
bclozel committed Feb 13, 2025
1 parent 803df55 commit 5d615eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);

private boolean isStandardMode = false;
private boolean httpOkOnValidationErrors = false;


/**
Expand All @@ -65,29 +65,31 @@ public GraphQlHttpHandler(WebGraphQlHandler graphQlHandler, CodecConfigurer code
}

/**
* Return whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* <p>When enabled, this mode will use 4xx/5xx HTTP response status if an error occurs before
* Return whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts; for example, if JSON parsing, GraphQL document parsing,
* or GraphQL document validation fails. When disabled, behavior will remain consistent with the
* "application/json" response content type.
* or GraphQL document validation fail.
* <p>This option only applies to {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} responses,
* as legacy {@link MediaType#APPLICATION_JSON} responses always use HTTP 200 OK in such cases.
* Enabling this option means the server will not conform to the "GraphQL over HTTP specification".
* <p>By default, this is set to {@code false}.
* @since 1.4.0
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">GraphQL over HTTP specification</a>
*/
public boolean isStandardMode() {
return this.isStandardMode;
public boolean isHttpOkOnValidationErrors() {
return this.httpOkOnValidationErrors;
}

/**
* Set whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* @param standardMode whether the "standard mode" should be enabled
* Set whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts.
* @param httpOkOnValidationErrors whether "HTTP 200 OK" responses should always be used
* @since 1.4.0
* @see #isStandardMode
* @deprecated since 1.4, will be made {@code false} permanently in a future release
* @see #isHttpOkOnValidationErrors
*/
public void setStandardMode(boolean standardMode) {
this.isStandardMode = standardMode;
@Deprecated(since = "1.4.0", forRemoval = true)
public void setHttpOkOnValidationErrors(boolean httpOkOnValidationErrors) {
this.httpOkOnValidationErrors = httpOkOnValidationErrors;
}

protected Mono<ServerResponse> prepareResponse(ServerRequest request, WebGraphQlResponse response) {
Expand All @@ -100,7 +102,7 @@ protected Mono<ServerResponse> prepareResponse(ServerRequest request, WebGraphQl
}

protected HttpStatus selectResponseStatus(WebGraphQlResponse response, MediaType responseMediaType) {
if (this.isStandardMode
if (!isHttpOkOnValidationErrors()
&& !response.getExecutionResult().isDataPresent()
&& MediaTypes.APPLICATION_GRAPHQL_RESPONSE.equals(responseMediaType)) {
return HttpStatus.BAD_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);

private boolean isStandardMode = false;
private boolean httpOkOnValidationErrors = false;

/**
* Create a new instance.
Expand All @@ -72,29 +72,31 @@ public GraphQlHttpHandler(WebGraphQlHandler graphQlHandler, @Nullable HttpMessag
}

/**
* Return whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* <p>When enabled, this mode will use 4xx/5xx HTTP response status if an error occurs before
* Return whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts; for example, if JSON parsing, GraphQL document parsing,
* or GraphQL document validation fails. When disabled, behavior will remain consistent with the
* "application/json" response content type.
* or GraphQL document validation fail.
* <p>This option only applies to {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} responses,
* as legacy {@link MediaType#APPLICATION_JSON} responses always use HTTP 200 OK in such cases.
* Enabling this option means the server will not conform to the "GraphQL over HTTP specification".
* <p>By default, this is set to {@code false}.
* @since 1.4.0
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">GraphQL over HTTP specification</a>
*/
public boolean isStandardMode() {
return this.isStandardMode;
public boolean isHttpOkOnValidationErrors() {
return this.httpOkOnValidationErrors;
}

/**
* Set whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* @param standardMode whether the "standard mode" should be enabled
* Set whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts.
* @param httpOkOnValidationErrors whether "HTTP 200 OK" responses should always be used
* @since 1.4.0
* @see #isStandardMode
* @deprecated since 1.4, will be made {@code false} permanently in a future release
* @see #isHttpOkOnValidationErrors
*/
public void setStandardMode(boolean standardMode) {
this.isStandardMode = standardMode;
@Deprecated(since = "1.4.0", forRemoval = true)
public void setHttpOkOnValidationErrors(boolean httpOkOnValidationErrors) {
this.httpOkOnValidationErrors = httpOkOnValidationErrors;
}


Expand Down Expand Up @@ -129,7 +131,7 @@ protected ServerResponse prepareResponse(ServerRequest request, Mono<WebGraphQlR
}

protected HttpStatus selectResponseStatus(WebGraphQlResponse response, MediaType responseMediaType) {
if (this.isStandardMode
if (!isHttpOkOnValidationErrors()
&& !response.getExecutionResult().isDataPresent()
&& MediaTypes.APPLICATION_GRAPHQL_RESPONSE.equals(responseMediaType)) {
return HttpStatus.BAD_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ static WebTestClient createTestClient(WebGraphQlSetup graphQlSetup) {
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
reader.register(WebFluxTestConfig.class);
GraphQlHttpHandler httpHandler = graphQlSetup.toHttpHandlerWebFlux();
httpHandler.setStandardMode(true);
RouterFunction<ServerResponse> routerFunction = RouterFunctions
.route()
.POST("/graphql", RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ static MockMvcTester createMvcTester(WebGraphQlSetup graphQlSetup) {
reader.register(MvcTestConfig.class);
context.setServletContext(new MockServletContext());
GraphQlHttpHandler httpHandler = graphQlSetup.toHttpHandler();
httpHandler.setStandardMode(true);
RouterFunction<ServerResponse> routerFunction = RouterFunctions
.route()
.POST("/graphql", RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE),
Expand Down

0 comments on commit 5d615eb

Please sign in to comment.