Search in sources :

Example 1 with JsonResponseWrapper

use of com.graphql_java_generator.client.response.JsonResponseWrapper in project graphql-maven-plugin-project by graphql-java-generator.

the class RestTemplateQueryExecutor method doJsonRequestExecution.

protected <T> T doJsonRequestExecution(String jsonRequest, Class<T> valueType) throws IOException, GraphQLRequestExecutionException {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<String>(jsonRequest, headers);
    JsonResponseWrapper response = this.restTemplate.postForEntity(graphqlEndpoint, entity, JsonResponseWrapper.class).getBody();
    if (logger.isInfoEnabled()) {
        logger.trace("Parsed response data: {}", objectMapper.writeValueAsString(response.data));
        logger.trace("Parsed response errors: {}", objectMapper.writeValueAsString(response.errors));
    }
    if (response.errors == null || response.errors.size() == 0) {
        // No errors. Let's parse the data
        return objectMapper.treeToValue(response.data, valueType);
    } else {
        int nbErrors = 0;
        String agregatedMessage = null;
        for (com.graphql_java_generator.client.response.Error error : response.errors) {
            String msg = error.toString();
            nbErrors += 1;
            logger.error(GRAPHQL_MARKER, msg);
            if (agregatedMessage == null) {
                agregatedMessage = msg;
            } else {
                agregatedMessage += ", ";
                agregatedMessage += msg;
            }
        }
        if (nbErrors == 0) {
            throw new GraphQLRequestExecutionException("An unknown error occured");
        } else {
            throw new GraphQLRequestExecutionException(nbErrors + " errors occured: " + agregatedMessage);
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) JsonResponseWrapper(com.graphql_java_generator.client.response.JsonResponseWrapper) HttpEntity(org.springframework.http.HttpEntity) GraphQLRequestExecutionException(com.graphql_java_generator.exception.GraphQLRequestExecutionException)

Example 2 with JsonResponseWrapper

use of com.graphql_java_generator.client.response.JsonResponseWrapper in project graphql-maven-plugin-project by graphql-java-generator.

the class RequestExecutionImpl method execute.

/**
 * {@inheritDoc}
 */
@Override
public <R extends GraphQLRequestObject> R execute(AbstractGraphQLRequest graphQLRequest, Map<String, Object> parameters, Class<R> dataResponseType) throws GraphQLRequestExecutionException {
    if (graphQLRequest.getRequestType().equals(RequestType.subscription))
        throw new GraphQLRequestExecutionException("This method may not be called for subscriptions");
    String jsonRequest = graphQLRequest.buildRequestAsString(parameters);
    try {
        logger.trace(GRAPHQL_MARKER, "Executing GraphQL request: {}", jsonRequest);
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
        JsonResponseWrapper response = invocationBuilder.post(Entity.entity(jsonRequest, MediaType.APPLICATION_JSON), JsonResponseWrapper.class);
        return RequestExecutionSpringReactiveImpl.parseDataFromGraphQLServerResponse(graphQLRequest.getGraphQLObjectMapper(), response, dataResponseType);
    } catch (IOException e) {
        throw new GraphQLRequestExecutionException("Error when executing query <" + jsonRequest + ">: " + e.getMessage(), e);
    }
}
Also used : JsonResponseWrapper(com.graphql_java_generator.client.response.JsonResponseWrapper) Invocation(javax.ws.rs.client.Invocation) GraphQLRequestExecutionException(com.graphql_java_generator.exception.GraphQLRequestExecutionException) IOException(java.io.IOException)

Example 3 with JsonResponseWrapper

use of com.graphql_java_generator.client.response.JsonResponseWrapper in project graphql-maven-plugin-project by graphql-java-generator.

the class RequestExecutionSpringReactiveImpl method execute.

@Override
public <R extends GraphQLRequestObject> R execute(AbstractGraphQLRequest graphQLRequest, Map<String, Object> parameters, Class<R> dataResponseType) throws GraphQLRequestExecutionException {
    String jsonRequest = "not initialized yet";
    if (graphQLRequest.getRequestType().equals(RequestType.subscription))
        throw new GraphQLRequestExecutionException("This method may not be called for subscriptions");
    try {
        Map<String, Object> map = graphQLRequest.buildRequestAsMap(parameters);
        jsonRequest = graphQLRequest.getGraphQLObjectMapper().writeValueAsString(map);
        logger.trace(GRAPHQL_MARKER, "Executing GraphQL request: {}", jsonRequest);
        JsonResponseWrapper responseJson = // 
        webClient.post().contentType(// 
        MediaType.APPLICATION_JSON).body(Mono.just(jsonRequest), // 
        String.class).accept(// 
        MediaType.APPLICATION_JSON).retrieve().bodyToMono(// 
        JsonResponseWrapper.class).block();
        return parseDataFromGraphQLServerResponse(graphQLRequest.getGraphQLObjectMapper(), responseJson, dataResponseType);
    } catch (IOException e) {
        throw new GraphQLRequestExecutionException("Error when executing query <" + jsonRequest + ">: " + e.getMessage(), e);
    }
}
Also used : JsonResponseWrapper(com.graphql_java_generator.client.response.JsonResponseWrapper) GraphQLRequestExecutionException(com.graphql_java_generator.exception.GraphQLRequestExecutionException) IOException(java.io.IOException)

Aggregations

JsonResponseWrapper (com.graphql_java_generator.client.response.JsonResponseWrapper)3 GraphQLRequestExecutionException (com.graphql_java_generator.exception.GraphQLRequestExecutionException)3 IOException (java.io.IOException)2 Invocation (javax.ws.rs.client.Invocation)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1