diff --git a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/JsonSerializer.java b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/JsonSerializer.java new file mode 100644 index 0000000..28e0044 --- /dev/null +++ b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/JsonSerializer.java @@ -0,0 +1,28 @@ +package graphql.spring.web.reactive; + +import graphql.PublicSpi; + +/** + * An interface for serializing and deserializing GraphQL objects. + */ +@PublicSpi +public interface JsonSerializer { + + /** + * Serializes the given object to a json {@link String}. + * + * @param object the object to serialize + * @return the json string + */ + String serialize(Object object); + + /** + * Deserializes the given json {@link String} to an object of the required type. + * + * @param json the json string + * @param requiredType the required type + * @param the required generic type + * @return the object + */ + T deserialize(String json, Class requiredType); +} diff --git a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/GraphQLController.java b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/GraphQLController.java index 7281799..552129f 100644 --- a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/GraphQLController.java +++ b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/GraphQLController.java @@ -1,11 +1,11 @@ package graphql.spring.web.reactive.components; -import com.fasterxml.jackson.databind.ObjectMapper; import graphql.ExecutionResult; import graphql.Internal; import graphql.spring.web.reactive.ExecutionResultHandler; import graphql.spring.web.reactive.GraphQLInvocation; import graphql.spring.web.reactive.GraphQLInvocationData; +import graphql.spring.web.reactive.JsonSerializer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -20,7 +20,6 @@ import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; -import java.io.IOException; import java.util.Collections; import java.util.Map; @@ -35,7 +34,7 @@ public class GraphQLController { ExecutionResultHandler executionResultHandler; @Autowired - ObjectMapper objectMapper; + JsonSerializer jsonSerializer; @RequestMapping(value = "${graphql.url:graphql}", method = RequestMethod.POST, @@ -46,7 +45,7 @@ public Object graphqlPOST( @RequestParam(value = "operationName", required = false) String operationName, @RequestParam(value = "variables", required = false) String variablesJson, @RequestBody(required = false) String body, - ServerWebExchange serverWebExchange) throws IOException { + ServerWebExchange serverWebExchange) { if (body == null) { body = ""; @@ -64,7 +63,7 @@ public Object graphqlPOST( // } if (MediaType.APPLICATION_JSON_VALUE.equals(contentType)) { - GraphQLRequestBody request = objectMapper.readValue(body, GraphQLRequestBody.class); + GraphQLRequestBody request = jsonSerializer.deserialize(body, GraphQLRequestBody.class); if (request.getQuery() == null) { request.setQuery(""); } @@ -122,13 +121,10 @@ public Object graphqlGET( } private Map convertVariablesJson(String jsonMap) { - if (jsonMap == null) return Collections.emptyMap(); - try { - return objectMapper.readValue(jsonMap, Map.class); - } catch (IOException e) { - throw new RuntimeException("Could not convert variables GET parameter: expected a JSON map", e); + if (jsonMap == null) { + return Collections.emptyMap(); } - + return jsonSerializer.deserialize(jsonMap, Map.class); } private Object executeRequest( diff --git a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/JacksonJsonSerializer.java b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/JacksonJsonSerializer.java new file mode 100644 index 0000000..98f0197 --- /dev/null +++ b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/JacksonJsonSerializer.java @@ -0,0 +1,37 @@ +package graphql.spring.web.reactive.components; + +import com.fasterxml.jackson.databind.ObjectMapper; +import graphql.spring.web.reactive.JsonSerializer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class JacksonJsonSerializer implements JsonSerializer { + + private ObjectMapper objectMapper; + + @Autowired + public JacksonJsonSerializer(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + @Override + public String serialize(Object object) { + try { + return objectMapper.writeValueAsString(object); + } catch (IOException e) { + throw new RuntimeException("Error serializing object to JSON: " + e.getMessage(), e); + } + } + + @Override + public T deserialize(String json, Class requiredType) { + try { + return objectMapper.readValue(json, requiredType); + } catch (IOException e) { + throw new RuntimeException("Error deserializing object from JSON: " + e.getMessage(), e); + } + } +} diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/JsonSerializer.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/JsonSerializer.java new file mode 100644 index 0000000..7992e84 --- /dev/null +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/JsonSerializer.java @@ -0,0 +1,28 @@ +package graphql.spring.web.servlet; + +import graphql.PublicSpi; + +/** + * An interface for serializing and deserializing GraphQL objects. + */ +@PublicSpi +public interface JsonSerializer { + + /** + * Serializes the given object to a json {@link String}. + * + * @param object the object to serialize + * @return the json string + */ + String serialize(Object object); + + /** + * Deserializes the given json {@link String} to an object of the required type. + * + * @param json the json string + * @param requiredType the required type + * @param the required generic type + * @return the object + */ + T deserialize(String json, Class requiredType); +} diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/GraphQLController.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/GraphQLController.java index 700e8c5..9935b68 100644 --- a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/GraphQLController.java +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/GraphQLController.java @@ -1,11 +1,11 @@ package graphql.spring.web.servlet.components; -import com.fasterxml.jackson.databind.ObjectMapper; import graphql.ExecutionResult; import graphql.Internal; import graphql.spring.web.servlet.ExecutionResultHandler; import graphql.spring.web.servlet.GraphQLInvocation; import graphql.spring.web.servlet.GraphQLInvocationData; +import graphql.spring.web.servlet.JsonSerializer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -35,7 +35,7 @@ public class GraphQLController { ExecutionResultHandler executionResultHandler; @Autowired - ObjectMapper objectMapper; + JsonSerializer jsonSerializer; @RequestMapping(value = "${graphql.url:graphql}", method = RequestMethod.POST, @@ -64,7 +64,7 @@ public Object graphqlPOST( // } if (MediaType.APPLICATION_JSON_VALUE.equals(contentType)) { - GraphQLRequestBody request = objectMapper.readValue(body, GraphQLRequestBody.class); + GraphQLRequestBody request = jsonSerializer.deserialize(body, GraphQLRequestBody.class); if (request.getQuery() == null) { request.setQuery(""); } @@ -122,13 +122,10 @@ public Object graphqlGET( } private Map convertVariablesJson(String jsonMap) { - if (jsonMap == null) return Collections.emptyMap(); - try { - return objectMapper.readValue(jsonMap, Map.class); - } catch (IOException e) { - throw new RuntimeException("Could not convert variables GET parameter: expected a JSON map", e); + if (jsonMap == null) { + return Collections.emptyMap(); } - + return jsonSerializer.deserialize(jsonMap, Map.class); } private Object executeRequest( diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/JacksonJsonSerializer.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/JacksonJsonSerializer.java new file mode 100644 index 0000000..7c5d777 --- /dev/null +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/JacksonJsonSerializer.java @@ -0,0 +1,37 @@ +package graphql.spring.web.servlet.components; + +import com.fasterxml.jackson.databind.ObjectMapper; +import graphql.spring.web.servlet.JsonSerializer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class JacksonJsonSerializer implements JsonSerializer { + + private ObjectMapper objectMapper; + + @Autowired + public JacksonJsonSerializer(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + @Override + public String serialize(Object object) { + try { + return objectMapper.writeValueAsString(object); + } catch (IOException e) { + throw new RuntimeException("Error serializing object to JSON: " + e.getMessage(), e); + } + } + + @Override + public T deserialize(String json, Class requiredType) { + try { + return objectMapper.readValue(json, requiredType); + } catch (IOException e) { + throw new RuntimeException("Error deserializing object from JSON: " + e.getMessage(), e); + } + } +}