use of org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse in project besu by hyperledger.
the class GraphQLHttpService method process.
private GraphQLResponse process(final String requestJson, final String operationName, final Map<String, Object> variables) {
Map<GraphQLContextType, Object> contextMap = new ConcurrentHashMap<>();
contextMap.putAll(graphQlContextMap);
contextMap.put(GraphQLContextType.IS_ALIVE_HANDLER, new IsAliveHandler(scheduler, config.getHttpTimeoutSec()));
final ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(requestJson).operationName(operationName).variables(variables).graphQLContext(contextMap).build();
final ExecutionResult result = graphQL.execute(executionInput);
final Map<String, Object> toSpecificationResult = result.toSpecification();
final List<GraphQLError> errors = result.getErrors();
if (errors.size() == 0) {
return new GraphQLSuccessResponse(toSpecificationResult);
} else {
return new GraphQLErrorResponse(toSpecificationResult);
}
}
use of org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse in project besu by hyperledger.
the class GraphQLHttpService method handleGraphQLError.
private void handleGraphQLError(final RoutingContext routingContext, final Exception ex) {
LOG.debug("Error handling GraphQL request", ex);
final HttpServerResponse response = routingContext.response();
if (!response.closed()) {
response.setStatusCode(HttpResponseStatus.BAD_REQUEST.code()).end(Json.encode(new GraphQLErrorResponse(ex.getMessage())));
}
}
use of org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse in project besu by hyperledger.
the class GraphQLHttpService method handleGraphQLRequest.
private void handleGraphQLRequest(final RoutingContext routingContext) {
try {
final String query;
final String operationName;
final Map<String, Object> variables;
final HttpServerRequest request = routingContext.request();
switch(request.method().name()) {
case "GET":
final String queryString = request.getParam("query");
query = Objects.requireNonNullElse(queryString, "");
operationName = request.getParam("operationName");
final String variableString = request.getParam("variables");
if (variableString != null) {
variables = JacksonCodec.decodeValue(variableString, MAP_TYPE);
} else {
variables = Collections.emptyMap();
}
break;
case "POST":
final String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
if (contentType != null && MediaType.parse(contentType).is(MEDIA_TYPE_JUST_JSON)) {
final String requestBody = routingContext.getBodyAsString().trim();
final GraphQLJsonRequest jsonRequest = Json.decodeValue(requestBody, GraphQLJsonRequest.class);
final String jsonQuery = jsonRequest.getQuery();
query = Objects.requireNonNullElse(jsonQuery, "");
operationName = jsonRequest.getOperationName();
final Map<String, Object> jsonVariables = jsonRequest.getVariables();
variables = Objects.requireNonNullElse(jsonVariables, Collections.emptyMap());
} else {
// treat all else as application/graphql
final String requestQuery = routingContext.getBodyAsString().trim();
query = Objects.requireNonNullElse(requestQuery, "");
operationName = null;
variables = Collections.emptyMap();
}
break;
default:
routingContext.response().setStatusCode(HttpResponseStatus.METHOD_NOT_ALLOWED.code()).end();
return;
}
final HttpServerResponse response = routingContext.response();
vertx.executeBlocking(future -> {
try {
final GraphQLResponse graphQLResponse = process(query, operationName, variables);
future.complete(graphQLResponse);
} catch (final Exception e) {
future.fail(e);
}
}, false, (res) -> {
if (response.closed()) {
return;
}
response.putHeader("Content-Type", MediaType.JSON_UTF_8.toString());
if (res.failed()) {
response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
response.end(serialise(new GraphQLErrorResponse(Collections.singletonMap("errors", Collections.singletonList(Collections.singletonMap("message", res.cause().getMessage()))))));
} else {
final GraphQLResponse graphQLResponse = (GraphQLResponse) res.result();
response.setStatusCode(status(graphQLResponse).code());
response.end(serialise(graphQLResponse));
}
});
} catch (final DecodeException ex) {
handleGraphQLError(routingContext, ex);
}
}
Aggregations