graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation Java Examples

The following examples show how to use graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GraphQLProvider.java    From graphql-java-examples with MIT License 6 votes vote down vote up
@PostConstruct
public void init() throws IOException {
    URL url = Resources.getResource("starWarsSchemaAnnotated.graphqls");
    String sdl = Resources.toString(url, Charsets.UTF_8);
    GraphQLSchema graphQLSchema = buildSchema(sdl);

    //
    // 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.
    //
    DataLoaderDispatcherInstrumentation dlInstrumentation =
            new DataLoaderDispatcherInstrumentation(dataLoaderRegistry, newOptions().includeStatistics(true));

    Instrumentation instrumentation = new ChainedInstrumentation(
            asList(new TracingInstrumentation(), dlInstrumentation)
    );


    this.graphQL = GraphQL.newGraphQL(graphQLSchema).instrumentation(instrumentation).build();
}
 
Example #2
Source File: VertxMappedBatchLoaderTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();

  SchemaParser schemaParser = new SchemaParser();
  TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("allLinks", this::getAllLinks))
    .type("Link", builder -> builder.dataFetcher("postedBy", this::getLinkPostedBy))
    .build();

  SchemaGenerator schemaGenerator = new SchemaGenerator();
  GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
  DataLoaderDispatcherInstrumentation dispatcherInstrumentation = new DataLoaderDispatcherInstrumentation();

  return GraphQL.newGraphQL(graphQLSchema)
    .instrumentation(dispatcherInstrumentation)
    .build();
}
 
Example #3
Source File: VertxBatchLoaderTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();

  SchemaParser schemaParser = new SchemaParser();
  TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("allLinks", this::getAllLinks))
    .type("Link", builder -> builder.dataFetcher("postedBy", this::getLinkPostedBy))
    .build();

  SchemaGenerator schemaGenerator = new SchemaGenerator();
  GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
  DataLoaderDispatcherInstrumentation dispatcherInstrumentation = new DataLoaderDispatcherInstrumentation();

  return GraphQL.newGraphQL(graphQLSchema)
    .instrumentation(dispatcherInstrumentation)
    .build();
}
 
Example #4
Source File: HttpMain.java    From graphql-java-http-example with MIT License 4 votes vote down vote up
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());

    //
    // 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.
    //
    StarWarsWiring.Context context = new StarWarsWiring.Context();
    executionInput.context(context);

    //
    // you need a schema in order to execute queries
    GraphQLSchema schema = buildStarWarsSchema();

    //
    // 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 = context.getDataLoaderRegistry();


    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 is pluggable
            .instrumentation(instrumentation)
            .build();
    ExecutionResult executionResult = graphQL.execute(executionInput.build());

    returnAsJson(httpResponse, executionResult);
}