Search in sources :

Example 1 with TracingInstrumentation

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);
}
Also used : ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) HashMap(java.util.HashMap) GraphQL(graphql.GraphQL) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) GraphQLSchema(graphql.schema.GraphQLSchema) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) ExecutionInput.newExecutionInput(graphql.ExecutionInput.newExecutionInput) ExecutionInput(graphql.ExecutionInput)

Aggregations

ExecutionInput (graphql.ExecutionInput)1 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)1 ExecutionResult (graphql.ExecutionResult)1 GraphQL (graphql.GraphQL)1 ChainedInstrumentation (graphql.execution.instrumentation.ChainedInstrumentation)1 Instrumentation (graphql.execution.instrumentation.Instrumentation)1 DataLoaderDispatcherInstrumentation (graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation)1 TracingInstrumentation (graphql.execution.instrumentation.tracing.TracingInstrumentation)1 GraphQLSchema (graphql.schema.GraphQLSchema)1 HashMap (java.util.HashMap)1 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)1