Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/main/java/graphql/servlet/GraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
this.fileUpload = new ServletFileUpload(fileItemFactory != null ? fileItemFactory : new DiskFileItemFactory());

this.getHandler = (request, response) -> {
GraphQLContext context = createContext(Optional.of(request), Optional.of(response));
final GraphQLContext context = createContext(Optional.of(request), Optional.of(response));
String path = request.getPathInfo();
if (path == null) {
path = request.getServletPath();
Expand All @@ -101,7 +101,7 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
query(CharStreams.toString(new InputStreamReader(GraphQLServlet.class.getResourceAsStream("introspectionQuery"))), null, new HashMap<>(), getSchema(), request, response, context);
} else {
if (request.getParameter("query") != null) {
Map<String, Object> variables = new HashMap<>();
final Map<String, Object> variables = new HashMap<>();
if (request.getParameter("variables") != null) {
variables.putAll(mapper.readValue(request.getParameter("variables"), new TypeReference<Map<String, Object>>() { }));
}
Expand All @@ -118,43 +118,43 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
};

this.postHandler = (request, response) -> {
GraphQLContext context = createContext(Optional.of(request), Optional.of(response));
final GraphQLContext context = createContext(Optional.of(request), Optional.of(response));
GraphQLRequest graphQLRequest = null;

try {
InputStream inputStream = null;

if (ServletFileUpload.isMultipartContent(request)) {
Map<String, List<FileItem>> fileItems = fileUpload.parseParameterMap(request);
final Map<String, List<FileItem>> fileItems = fileUpload.parseParameterMap(request);

if (fileItems.containsKey("graphql")) {
Optional<FileItem> graphqlItem = getFileItem(fileItems, "graphql");
if(graphqlItem.isPresent()) {
final Optional<FileItem> graphqlItem = getFileItem(fileItems, "graphql");
if (graphqlItem.isPresent()) {
inputStream = graphqlItem.get().getInputStream();
}

} else if(fileItems.containsKey("query")) {
Optional<FileItem> queryItem = getFileItem(fileItems, "query");
if(queryItem.isPresent()) {
} else if (fileItems.containsKey("query")) {
final Optional<FileItem> queryItem = getFileItem(fileItems, "query");
if (queryItem.isPresent()) {
graphQLRequest = new GraphQLRequest();
graphQLRequest.setQuery(new String(queryItem.get().get()));

Optional<FileItem> operationNameItem = getFileItem(fileItems, "operationName");
if(operationNameItem.isPresent()) {
final Optional<FileItem> operationNameItem = getFileItem(fileItems, "operationName");
if (operationNameItem.isPresent()) {
graphQLRequest.setOperationName(new String(operationNameItem.get().get()).trim());
}

Optional<FileItem> variablesItem = getFileItem(fileItems, "variables");
if(variablesItem.isPresent()) {
final Optional<FileItem> variablesItem = getFileItem(fileItems, "variables");
if (variablesItem.isPresent()) {
String variables = new String(variablesItem.get().get());
if(!variables.isEmpty()) {
if (!variables.isEmpty()) {
graphQLRequest.setVariables((Map<String, Object>) mapper.readValue(variables, Map.class));
}
}
}
}

if(inputStream == null && graphQLRequest == null) {
if (inputStream == null && graphQLRequest == null) {
response.setStatus(STATUS_BAD_REQUEST);
log.info("Bad POST multipart request: no part named \"graphql\" or \"query\"");
return;
Expand All @@ -167,7 +167,7 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
inputStream = request.getInputStream();
}

if(graphQLRequest == null) {
if (graphQLRequest == null) {
graphQLRequest = mapper.readValue(inputStream, GraphQLRequest.class);
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public String[] getMutations() {
@Override @SneakyThrows
public String executeQuery(String query) {
try {
ExecutionResult result = new GraphQL(getSchema()).execute(query, createContext(Optional.empty(), Optional.empty()), new HashMap<>());
final ExecutionResult result = new GraphQL(getSchema()).execute(query, createContext(Optional.empty(), Optional.empty()), new HashMap<>());
return mapper.writeValueAsString(createResultFromDataAndErrors(result.getData(), result.getErrors()));
} catch (Exception e) {
return e.getMessage();
Expand Down Expand Up @@ -268,11 +268,11 @@ public Void run() {
} else {
runListeners(operationListeners, l -> runListener(l, it -> it.beforeGraphQLOperation(context, operationName, query, variables)));

ExecutionResult executionResult = new GraphQL(schema, getExecutionStrategy()).execute(query, operationName, context, transformVariables(schema, query, variables));
List<GraphQLError> errors = executionResult.getErrors();
Object data = executionResult.getData();
final ExecutionResult executionResult = new GraphQL(schema, getExecutionStrategy()).execute(query, operationName, context, transformVariables(schema, query, variables));
final List<GraphQLError> errors = executionResult.getErrors();
final Object data = executionResult.getData();

String response = mapper.writeValueAsString(createResultFromDataAndErrors(data, errors));
final String response = mapper.writeValueAsString(createResultFromDataAndErrors(data, errors));

resp.setContentType(APPLICATION_JSON_UTF8);
resp.setStatus(STATUS_OK);
Expand All @@ -288,12 +288,12 @@ public Void run() {

private Map<String, Object> createResultFromDataAndErrors(Object data, List<GraphQLError> errors) {

Map<String, Object> result = new HashMap<>();
final Map<String, Object> result = new HashMap<>();
result.put("data", data);

if (errorsPresent(errors)) {
List<GraphQLError> clientErrors = filterGraphQLErrors(errors);
if(clientErrors.size() < errors.size()) {
final List<GraphQLError> clientErrors = filterGraphQLErrors(errors);
if (clientErrors.size() < errors.size()) {
// Some errors were filtered out to hide implementation - put a generic error in place.
clientErrors.add(new GenericGraphQLError("Internal Server Error(s) while executing query"));
}
Expand All @@ -314,7 +314,7 @@ protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
}

private <T> void runListeners(List<T> listeners, Consumer<? super T> action) {
if(listeners != null) {
if (listeners != null) {
listeners.forEach(l -> runListener(l, action));
}
}
Expand All @@ -333,7 +333,7 @@ private <T> void runListener(T listener, Consumer<? super T> action) {
protected static class VariablesDeserializer extends JsonDeserializer<Map<String, Object>> {
@Override
public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Object o = p.readValueAs(Object.class);
final Object o = p.readValueAs(Object.class);
if (o instanceof Map) {
return (Map<String, Object>) o;
} else if (o instanceof String) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/graphql/servlet/OsgiGraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,28 @@
)
public class OsgiGraphQLServlet extends GraphQLServlet {

private List<GraphQLQueryProvider> queryProviders = new ArrayList<>();
private List<GraphQLMutationProvider> mutationProviders = new ArrayList<>();
private List<GraphQLTypesProvider> typesProviders = new ArrayList<>();
private final List<GraphQLQueryProvider> queryProviders = new ArrayList<>();
private final List<GraphQLMutationProvider> mutationProviders = new ArrayList<>();
private final List<GraphQLTypesProvider> typesProviders = new ArrayList<>();

private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider();

private GraphQLSchema schema;
private GraphQLSchema readOnlySchema;

protected void updateSchema() {
GraphQLObjectType.Builder object = newObject().name("Query").description("Root query type");
final GraphQLObjectType.Builder object = newObject().name("Query").description("Root query type");

for (GraphQLQueryProvider provider : queryProviders) {
if (provider.getQueries() != null && provider.getQueries().size() > 0) {
if (provider.getQueries() != null && !provider.getQueries().isEmpty()) {
for (GraphQLFieldDefinition graphQLFieldDefinition : provider.getQueries()) {
object.field(graphQLFieldDefinition);
}
}
}

Set<GraphQLType> types = new HashSet<>();
final Set<GraphQLType> types = new HashSet<>();
for (GraphQLTypesProvider typesProvider : typesProviders) {
types.addAll(typesProvider.getTypes());
}
Expand All @@ -72,14 +75,14 @@ protected void updateSchema() {
if (mutationProviders.isEmpty()) {
schema = readOnlySchema;
} else {
GraphQLObjectType.Builder mutationObject = newObject().name("Mutation").description("Root mutation type");
final GraphQLObjectType.Builder mutationObject = newObject().name("Mutation").description("Root mutation type");

for (GraphQLMutationProvider provider : mutationProviders) {
provider.getMutations().forEach(mutationObject::field);
}

GraphQLObjectType mutationType = mutationObject.build();
if (mutationType.getFieldDefinitions().size() == 0) {
final GraphQLObjectType mutationType = mutationObject.build();
if (mutationType.getFieldDefinitions().isEmpty()) {
schema = readOnlySchema;
} else {
schema = newSchema().query(object.build()).mutation(mutationType).build();
Expand Down Expand Up @@ -121,9 +124,6 @@ public void unbindTypesProvider(GraphQLTypesProvider typesProvider) {
updateSchema();
}

private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider();

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY)
public void setContextProvider(GraphQLContextBuilder contextBuilder) {
this.contextBuilder = contextBuilder;
Expand Down