graphql.servlet.GraphQLObjectMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphql-java-servlet Show documentation
Show all versions of graphql-java-servlet Show documentation
relay.js-compatible GraphQL servlet
The newest version!
package graphql.servlet;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLError;
import graphql.servlet.internal.GraphQLRequest;
import graphql.servlet.internal.VariablesDeserializer;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
* @author Andrew Potter
*/
public class GraphQLObjectMapper {
private final ObjectMapperProvider objectMapperProvider;
private final Supplier graphQLErrorHandlerSupplier;
private volatile ObjectMapper mapper;
protected GraphQLObjectMapper(ObjectMapperProvider objectMapperProvider, Supplier graphQLErrorHandlerSupplier) {
this.objectMapperProvider = objectMapperProvider;
this.graphQLErrorHandlerSupplier = graphQLErrorHandlerSupplier;
}
// Double-check idiom for lazy initialization of instance fields.
public ObjectMapper getJacksonMapper() {
ObjectMapper result = mapper;
if (result == null) { // First check (no locking)
synchronized(this) {
result = mapper;
if (result == null) // Second check (with locking)
mapper = result = objectMapperProvider.provide();
}
}
return result;
}
/**
* @return an {@link ObjectReader} for deserializing {@link GraphQLRequest}
*/
public ObjectReader getGraphQLRequestMapper() {
return getJacksonMapper().reader().forType(GraphQLRequest.class);
}
public GraphQLRequest readGraphQLRequest(InputStream inputStream) throws IOException {
return getGraphQLRequestMapper().readValue(inputStream);
}
public GraphQLRequest readGraphQLRequest(String text) throws IOException {
return getGraphQLRequestMapper().readValue(text);
}
public List readBatchedGraphQLRequest(InputStream inputStream) throws IOException {
MappingIterator iterator = getGraphQLRequestMapper().readValues(inputStream);
List requests = new ArrayList<>();
while (iterator.hasNext()) {
requests.add(iterator.next());
}
return requests;
}
public List readBatchedGraphQLRequest(String query) throws IOException {
MappingIterator iterator = getGraphQLRequestMapper().readValues(query);
List requests = new ArrayList<>();
while (iterator.hasNext()) {
requests.add(iterator.next());
}
return requests;
}
public String serializeResultAsJson(ExecutionResult executionResult) {
try {
return getJacksonMapper().writeValueAsString(createResultFromExecutionResult(executionResult));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public boolean areErrorsPresent(ExecutionResult executionResult) {
return graphQLErrorHandlerSupplier.get().errorsPresent(executionResult.getErrors());
}
public ExecutionResult sanitizeErrors(ExecutionResult executionResult) {
Object data = executionResult.getData();
Map