|
14 | 14 | */ |
15 | 15 | package graphql.servlet; |
16 | 16 |
|
17 | | -import com.fasterxml.jackson.annotation.JsonProperty; |
| 17 | +import com.fasterxml.jackson.annotation.JsonGetter; |
| 18 | +import com.fasterxml.jackson.core.JsonParser; |
| 19 | +import com.fasterxml.jackson.core.JsonProcessingException; |
18 | 20 | import com.fasterxml.jackson.core.type.TypeReference; |
| 21 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 22 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
19 | 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.RuntimeJsonMappingException; |
| 25 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
20 | 26 | import com.google.common.io.CharStreams; |
21 | 27 | import graphql.*; |
22 | 28 | import graphql.schema.GraphQLFieldDefinition; |
@@ -162,23 +168,29 @@ public String executeQuery(String query) { |
162 | 168 | } |
163 | 169 | } |
164 | 170 |
|
| 171 | + private static class VariablesDeserializer extends JsonDeserializer<Map<String, Object>> { |
| 172 | + |
| 173 | + @Override public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt) |
| 174 | + throws IOException { |
| 175 | + Object o = p.readValueAs(Object.class); |
| 176 | + if (o instanceof Map) { |
| 177 | + return (Map<String, Object>) o; |
| 178 | + } else if (o instanceof String) { |
| 179 | + return new ObjectMapper().readValue((String) o, new TypeReference<Map<String, Object>>() {}); |
| 180 | + } else { |
| 181 | + throw new RuntimeJsonMappingException("variables should be either an object or a string"); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
165 | 186 | public static class Request { |
166 | 187 | @Getter @Setter |
167 | 188 | private String query; |
168 | | - @Getter |
| 189 | + @Getter @Setter @JsonDeserialize(using = VariablesDeserializer.class) |
169 | 190 | private Map<String, Object> variables = new HashMap<>(); |
170 | 191 | @Getter @Setter |
171 | 192 | private String operationName; |
172 | 193 |
|
173 | | - @SneakyThrows @JsonProperty("variables") |
174 | | - public String getVariablesAsString() { |
175 | | - return new ObjectMapper().writeValueAsString(variables); |
176 | | - } |
177 | | - |
178 | | - @SneakyThrows |
179 | | - public void setVariables(String variables) { |
180 | | - this.variables = new ObjectMapper().readValue(variables, new TypeReference<Map<String, Object>>() {}); |
181 | | - } |
182 | 194 | } |
183 | 195 |
|
184 | 196 | @Override |
|
0 commit comments