use of com.yahoo.elide.graphql.parser.GraphQLEntityProjectionMaker in project elide by yahoo.
the class GraphQLTableExportOperation method getProjections.
@Override
public Collection<EntityProjection> getProjections(TableExport export, RequestScope scope) {
GraphQLProjectionInfo projectionInfo;
try {
String graphQLDocument = export.getQuery();
Elide elide = getService().getElide();
ObjectMapper mapper = elide.getMapper().getObjectMapper();
JsonNode node = QueryRunner.getTopLevelNode(mapper, graphQLDocument);
Map<String, Object> variables = QueryRunner.extractVariables(mapper, node);
String queryString = QueryRunner.extractQuery(node);
projectionInfo = new GraphQLEntityProjectionMaker(elide.getElideSettings(), variables, scope.getApiVersion()).make(queryString);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return projectionInfo.getProjections().values();
}
use of com.yahoo.elide.graphql.parser.GraphQLEntityProjectionMaker in project elide by yahoo.
the class QueryRunner method executeGraphQLRequest.
private ElideResponse executeGraphQLRequest(String baseUrlEndPoint, ObjectMapper mapper, User principal, String graphQLDocument, GraphQLQuery query, UUID requestId, Map<String, List<String>> requestHeaders) {
boolean isVerbose = false;
String queryText = query.getQuery();
boolean isMutation = isMutation(queryText);
try (DataStoreTransaction tx = isMutation ? elide.getDataStore().beginTransaction() : elide.getDataStore().beginReadTransaction()) {
elide.getTransactionRegistry().addRunningTransaction(requestId, tx);
if (query.getQuery() == null || query.getQuery().isEmpty()) {
return ElideResponse.builder().responseCode(HttpStatus.SC_BAD_REQUEST).body("A `query` key is required.").build();
}
// get variables from request for constructing entityProjections
Map<String, Object> variables = query.getVariables();
// TODO - get API version.
GraphQLProjectionInfo projectionInfo = new GraphQLEntityProjectionMaker(elide.getElideSettings(), variables, apiVersion).make(queryText);
GraphQLRequestScope requestScope = new GraphQLRequestScope(baseUrlEndPoint, tx, principal, apiVersion, elide.getElideSettings(), projectionInfo, requestId, requestHeaders);
isVerbose = requestScope.getPermissionExecutor().isVerbose();
// Logging all queries. It is recommended to put any private information that shouldn't be logged into
// the "variables" section of your query. Variable values are not logged.
log.info("Processing GraphQL query:\n{}", queryText);
ExecutionInput.Builder executionInput = new ExecutionInput.Builder().localContext(requestScope).query(queryText);
if (query.getOperationName() != null) {
executionInput.operationName(query.getOperationName());
}
executionInput.variables(variables);
ExecutionResult result = api.execute(executionInput);
tx.preCommit(requestScope);
requestScope.getPermissionExecutor().executeCommitChecks();
if (isMutation) {
if (!result.getErrors().isEmpty()) {
HashMap<String, Object> abortedResponseObject = new HashMap<>();
abortedResponseObject.put("errors", result.getErrors());
abortedResponseObject.put("data", null);
// Do not commit. Throw OK response to process tx.close correctly.
throw new WebApplicationException(Response.ok(mapper.writeValueAsString(abortedResponseObject)).build());
}
requestScope.saveOrCreateObjects();
}
tx.flush(requestScope);
requestScope.runQueuedPreCommitTriggers();
elide.getAuditLogger().commit();
tx.commit(requestScope);
requestScope.runQueuedPostCommitTriggers();
if (log.isTraceEnabled()) {
requestScope.getPermissionExecutor().logCheckStats();
}
return ElideResponse.builder().responseCode(HttpStatus.SC_OK).body(mapper.writeValueAsString(result)).build();
} catch (JsonProcessingException e) {
log.debug("Invalid json body provided to GraphQL", e);
return buildErrorResponse(mapper, new InvalidEntityBodyException(graphQLDocument), isVerbose);
} catch (IOException e) {
log.error("Uncaught IO Exception by Elide in GraphQL", e);
return buildErrorResponse(mapper, new TransactionException(e), isVerbose);
} catch (RuntimeException e) {
return handleRuntimeException(elide, e, isVerbose);
} finally {
elide.getTransactionRegistry().removeRunningTransaction(requestId);
elide.getAuditLogger().clear();
}
}
Aggregations