Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace batching strategy with DataLoader #432

Closed
kaqqao opened this issue Nov 17, 2022 · 1 comment
Closed

Replace batching strategy with DataLoader #432

kaqqao opened this issue Nov 17, 2022 · 1 comment

Comments

@kaqqao
Copy link
Member

kaqqao commented Nov 17, 2022

graphql-java removed BatchedExecutionStrategy and @Batched annotation. In order to build with newer versions of graphql-java, yet still support batching and preserve backwards-compatibility as much as possible, SPQR must reimplement batching via DataLoader (the only currently supported batching strategy in graphql-java).

To make use of the new implementation:

  1. Replace graphql.execution.batched.Batched with io.leangen.graphql.annotations.Batched (same @Batched, different package)
  2. Configure DataLoaderRegistry either:
    a. usingGraphQLRuntime.newGraphQL instead of GraphQL.newGraphQL
    b. manually, using the generated BatchLoaderWithContext instances

Examples:

a)

//No changes in the business logic, apart from the new annotation package
public class UserService {
    @Batched
    @GraphQLQuery
    //Can also return List<Education>, but async fetching is preferred
    public CompletionStage<List<Education>> education(@GraphQLContext List<User> users) {
        ... //Batch-resolve the educations for all users
    }
}

//ExecutableSchema contains the schema and the batch loaders
ExecutableSchema executableSchema = new GraphQLSchemaGenerator()
          .withOperationsFromSingleton(new UserService());
          .generateExecutable();

GraphQL graphQL = GraphQLRuntime.newGraphQL(executableSchema).build();

//Execute queries as normal, no DataLoader setup needed
ExecutionResult result = graphQL.execute("{users {education {startYear}}}");

b)

ExecutableSchema executableSchema = new GraphQLSchemaGenerator()
          .withOperationsFromSingleton(new UserService());
          .generateExecutable();

GraphQL graphQL = GraphQL.newGraphQL(executableSchema.getSchema()).build();

//Get the generated BatchLoaderWithContext instances and setup DataLoader manually
DataLoaderRegistry registry = new DataLoaderRegistry();
executableSchema.getBatchLoaders().forEach((loaderName, batchLoader) -> {
    //Configure DataLoaderOptions etc as needed
    registry.register(loaderName, DataLoader.newDataLoader(batchLoader));
});

ExecutionResult result = graphQL.execute(ExecutionInput.newExecutionInput()
                .query("{users {education {startYear}}}")
                .dataLoaderRegistry(registry) //Make sure to use the registry
                .build());
@kaqqao kaqqao closed this as completed in 75a2c2d Nov 17, 2022
@fedor-bobin-db
Copy link

@kaqqao Hi, could you please build new release version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants