use of graphql.GraphQL 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.GraphQL in project graphql-java by graphql-java.
the class BatchingExamples method perRequestGraphQl.
private void perRequestGraphQl() {
GraphQLSchema staticSchema = staticSchema_Or_MayBeFrom_IoC_Injection();
DataLoaderRegistry registry = new DataLoaderRegistry();
registry.register("character", getCharacterDataLoader());
DataLoaderDispatcherInstrumentation dispatcherInstrumentation = new DataLoaderDispatcherInstrumentation(registry);
GraphQL graphQL = GraphQL.newGraphQL(staticSchema).instrumentation(dispatcherInstrumentation).build();
graphQL.execute("{ helloworld }");
// you can now throw away the GraphQL and hence DataLoaderDispatcherInstrumentation
// and DataLoaderRegistry objects since they are really cheap to build per request
}
use of graphql.GraphQL 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.GraphQL in project graphql-java by graphql-java.
the class ExecutionExamples method exampleExecutorServiceExecutionStrategy.
private void exampleExecutorServiceExecutionStrategy() {
ExecutorService executorService = new ThreadPoolExecutor(2, /* core pool size 2 thread */
2, /* max pool size 2 thread */
30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
GraphQL graphQL = GraphQL.newGraphQL(StarWarsSchema.starWarsSchema).queryExecutionStrategy(new ExecutorServiceExecutionStrategy(executorService)).mutationExecutionStrategy(new AsyncSerialExecutionStrategy()).build();
}
use of graphql.GraphQL 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