use of graphql.ExecutionResult in project admin-console-beta by connexta.
the class GraphQLServlet method query.
private void query(String query, String operationName, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {
if (Subject.getSubject(AccessController.getContext()) == null && context.getSubject().isPresent()) {
Subject.doAs(context.getSubject().get(), new PrivilegedAction<Void>() {
@Override
@SneakyThrows
public Void run() {
query(query, operationName, variables, schema, req, resp, context);
return null;
}
});
} else {
runListeners(operationListeners, l -> runListener(l, it -> it.beforeGraphQLOperation(context, operationName, query, variables)));
ExecutionResult executionResult = new GraphQL(schema, getQueryExecutionStrategy(), getMutationExecutionStrategy()).execute(query, operationName, context, transformVariables(schema, query, variables));
List<GraphQLError> errors = executionResult.getErrors();
Object data = executionResult.getData();
String response = mapper.writeValueAsString(createResultFromDataAndErrors(data, errors));
resp.setContentType(APPLICATION_JSON_UTF8);
resp.setStatus(STATUS_OK);
resp.getWriter().write(response);
if (errorsPresent(errors)) {
runListeners(operationListeners, l -> l.onFailedGraphQLOperation(context, operationName, query, variables, data, errors));
} else {
runListeners(operationListeners, l -> l.onSuccessfulGraphQLOperation(context, operationName, query, variables, data));
}
}
}
use of graphql.ExecutionResult in project admin-console-beta by connexta.
the class ExecutionStrategyImpl method execute.
@Override
public ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields) {
Map<String, Object> results = new LinkedHashMap<String, Object>();
for (String fieldName : fields.keySet()) {
List<Field> fieldList = fields.get(fieldName);
ExecutionResult resolvedResult = resolveField(executionContext, parentType, source, fieldList);
results.put(fieldName, resolvedResult != null ? resolvedResult.getData() : null);
}
return new ExecutionResultImpl(results, executionContext.getErrors());
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class DeferredExamples method basicExample.
void basicExample(HttpServletResponse httpServletResponse, String deferredQuery) {
GraphQLSchema schema = buildSchemaWithDirective();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
//
// deferredQuery contains the query with @defer directives in it
//
ExecutionResult initialResult = graphQL.execute(ExecutionInput.newExecutionInput().query(deferredQuery).build());
//
// then initial results happen first, the deferred ones will begin AFTER these initial
// results have completed
//
sendResult(httpServletResponse, initialResult);
Map<Object, Object> extensions = initialResult.getExtensions();
Publisher<ExecutionResult> deferredResults = (Publisher<ExecutionResult>) extensions.get(GraphQL.DEFERRED_RESULTS);
//
// you subscribe to the deferred results like any other reactive stream
//
deferredResults.subscribe(new Subscriber<ExecutionResult>() {
Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
subscription = s;
//
// how many you request is up to you
subscription.request(10);
}
@Override
public void onNext(ExecutionResult executionResult) {
//
// as each deferred result arrives, send it to where it needs to go
//
sendResult(httpServletResponse, executionResult);
subscription.request(10);
}
@Override
public void onError(Throwable t) {
handleError(httpServletResponse, t);
}
@Override
public void onComplete() {
completeResponse(httpServletResponse);
}
});
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class ExecutionExamples method toSpec.
public void toSpec() throws Exception {
ExecutionResult executionResult = graphQL.execute(executionInput);
Map<String, Object> toSpecificationResult = executionResult.toSpecification();
sendAsJson(toSpecificationResult);
}
use of graphql.ExecutionResult in project graphql-java by graphql-java.
the class ExecutionExamples method simpleQueryExecution.
private void simpleQueryExecution() throws Exception {
GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }").build();
ExecutionResult executionResult = graphQL.execute(executionInput);
Object data = executionResult.getData();
List<GraphQLError> errors = executionResult.getErrors();
}
Aggregations