use of graphql.execution.instrumentation.tracing.TracingInstrumentation in project graphql-java by graphql-java.
the class HttpMain method handleStarWars.
private void handleStarWars(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
//
// this builds out the parameters we need like the graphql query from the http request
QueryParameters parameters = QueryParameters.from(httpRequest);
if (parameters.getQuery() == null) {
//
// how to handle nonsensical requests is up to your application
httpResponse.setStatus(400);
return;
}
ExecutionInput.Builder executionInput = newExecutionInput().query(parameters.getQuery()).operationName(parameters.getOperationName()).variables(parameters.getVariables());
//
// This example uses the DataLoader technique to ensure that the most efficient
// loading of data (in this case StarWars characters) happens. We pass that to data
// fetchers via the graphql context object.
//
DataLoaderRegistry dataLoaderRegistry = buildDataLoaderRegistry();
//
// the context object is something that means something to down stream code. It is instructions
// from yourself to your other code such as DataFetchers. The engine passes this on unchanged and
// makes it available to inner code
//
// the graphql guidance says :
//
// - GraphQL should be placed after all authentication middleware, so that you
// - have access to the same session and user information you would in your
// - HTTP endpoint handlers.
//
Map<String, Object> context = new HashMap<>();
context.put("YouAppSecurityClearanceLevel", "CodeRed");
context.put("YouAppExecutingUser", "Dr Nefarious");
context.put("dataloaderRegistry", dataLoaderRegistry);
executionInput.context(context);
//
// you need a schema in order to execute queries
GraphQLSchema schema = buildStarWarsSchema();
DataLoaderDispatcherInstrumentation dlInstrumentation = new DataLoaderDispatcherInstrumentation(dataLoaderRegistry, newOptions().includeStatistics(true));
Instrumentation instrumentation = new ChainedInstrumentation(asList(new TracingInstrumentation(), dlInstrumentation));
// finally you build a runtime graphql object and execute the query
GraphQL graphQL = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
ExecutionResult executionResult = graphQL.execute(executionInput.build());
returnAsJson(httpResponse, executionResult);
}
Aggregations