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);
}
}
}
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);
}
}
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);
}
}
Aggregations