Search in sources :

Example 1 with GraphQLErrorResponse

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);
    }
}
Also used : GraphQLErrorResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse) ExecutionResult(graphql.ExecutionResult) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) GraphQLSuccessResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLSuccessResponse) GraphQLError(graphql.GraphQLError) ExecutionInput(graphql.ExecutionInput) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with GraphQLErrorResponse

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())));
    }
}
Also used : HttpServerResponse(io.vertx.core.http.HttpServerResponse) GraphQLErrorResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse)

Example 3 with GraphQLErrorResponse

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);
    }
}
Also used : GraphQLJsonRequest(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLJsonRequest) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) GraphQLErrorResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse) GraphQLResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLResponse) DecodeException(io.vertx.core.json.DecodeException) DecodeException(io.vertx.core.json.DecodeException) SocketException(java.net.SocketException)

Aggregations

GraphQLErrorResponse (org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 ExecutionInput (graphql.ExecutionInput)1 ExecutionResult (graphql.ExecutionResult)1 GraphQLError (graphql.GraphQLError)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 DecodeException (io.vertx.core.json.DecodeException)1 SocketException (java.net.SocketException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 GraphQLJsonRequest (org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLJsonRequest)1 GraphQLResponse (org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLResponse)1 GraphQLSuccessResponse (org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLSuccessResponse)1 IsAliveHandler (org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler)1