-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Jackson codec specifically for GraphQL HTTP endpoints
Prior to this commit, the `GraphQlHttpHandler` implementations would use the JSON codecs configured in the web Framework (MVC or WebFlux) for reading and writing GraphQL payloads as JSON documents. This can cause issues in cases the application configures the JSON codec in a way that makes it incompatible with the expected GraphQL documents. For example, not serializing empty values and arrays. This commit adds new constructors in `GraphQlHttpHandler` implementations that can get a custom JSON codec for GraphQL payloads. Closes gh-860
- Loading branch information
Showing
13 changed files
with
333 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
.../src/main/java/org/springframework/graphql/server/webflux/AbstractGraphQlHttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2020-2024 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.server.webflux; | ||
|
||
|
||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.graphql.server.WebGraphQlHandler; | ||
import org.springframework.graphql.server.support.SerializableGraphQlRequest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.reactive.function.server.ServerRequest; | ||
|
||
/** | ||
* Abstract class for GraphQL Handler implementations using the HTTP transport. | ||
* | ||
* @author Brian Clozel | ||
* @since 1.3.0 | ||
*/ | ||
class AbstractGraphQlHttpHandler { | ||
|
||
protected final WebGraphQlHandler graphQlHandler; | ||
|
||
@Nullable | ||
protected final HttpCodecDelegate codecDelegate; | ||
|
||
public AbstractGraphQlHttpHandler(WebGraphQlHandler graphQlHandler, @Nullable HttpCodecDelegate codecDelegate) { | ||
Assert.notNull(graphQlHandler, "WebGraphQlHandler is required"); | ||
this.graphQlHandler = graphQlHandler; | ||
this.codecDelegate = codecDelegate; | ||
} | ||
|
||
protected Mono<SerializableGraphQlRequest> readRequest(ServerRequest serverRequest) { | ||
if (this.codecDelegate != null) { | ||
MediaType contentType = serverRequest.headers().contentType().orElse(MediaType.APPLICATION_JSON); | ||
return this.codecDelegate.decode(serverRequest.bodyToFlux(DataBuffer.class), contentType); | ||
} | ||
else { | ||
return serverRequest.bodyToMono(SerializableGraphQlRequest.class); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...g-graphql/src/main/java/org/springframework/graphql/server/webflux/HttpCodecDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2002-2022 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.server.webflux; | ||
|
||
import java.util.Map; | ||
|
||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.codec.Decoder; | ||
import org.springframework.core.codec.Encoder; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.core.io.buffer.DefaultDataBufferFactory; | ||
import org.springframework.graphql.GraphQlResponse; | ||
import org.springframework.graphql.server.support.SerializableGraphQlRequest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.codec.CodecConfigurer; | ||
import org.springframework.http.codec.DecoderHttpMessageReader; | ||
import org.springframework.http.codec.EncoderHttpMessageWriter; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.MimeTypeUtils; | ||
|
||
/** | ||
* Helper class for encoding and decoding GraphQL messages in HTTP transport. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @author Brian Clozel | ||
* @since 1.3.0 | ||
*/ | ||
final class HttpCodecDelegate { | ||
|
||
private static final ResolvableType REQUEST_TYPE = ResolvableType.forClass(SerializableGraphQlRequest.class); | ||
|
||
private static final ResolvableType RESPONSE_TYPE = ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class); | ||
|
||
|
||
private final Decoder<?> decoder; | ||
|
||
private final Encoder<?> encoder; | ||
|
||
|
||
HttpCodecDelegate(CodecConfigurer codecConfigurer) { | ||
Assert.notNull(codecConfigurer, "CodecConfigurer is required"); | ||
this.decoder = findJsonDecoder(codecConfigurer); | ||
this.encoder = findJsonEncoder(codecConfigurer); | ||
} | ||
|
||
private static Decoder<?> findJsonDecoder(CodecConfigurer configurer) { | ||
return configurer.getReaders().stream() | ||
.filter((reader) -> reader.canRead(REQUEST_TYPE, MediaType.APPLICATION_JSON)) | ||
.map((reader) -> ((DecoderHttpMessageReader<?>) reader).getDecoder()) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("No JSON Decoder")); | ||
} | ||
|
||
private static Encoder<?> findJsonEncoder(CodecConfigurer configurer) { | ||
return configurer.getWriters().stream() | ||
.filter((writer) -> writer.canWrite(RESPONSE_TYPE, MediaType.APPLICATION_JSON)) | ||
.map((writer) -> ((EncoderHttpMessageWriter<?>) writer).getEncoder()) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("No JSON Encoder")); | ||
} | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
public DataBuffer encode(GraphQlResponse response) { | ||
return ((Encoder<Map<String, Object>>) this.encoder) | ||
.encodeValue(response.toMap(), DefaultDataBufferFactory.sharedInstance, RESPONSE_TYPE, MimeTypeUtils.APPLICATION_JSON, null); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Mono<SerializableGraphQlRequest> decode(Publisher<DataBuffer> inputStream, MediaType contentType) { | ||
return (Mono<SerializableGraphQlRequest>) this.decoder.decodeToMono(inputStream, REQUEST_TYPE, contentType, null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.