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:
Replace graphql.execution.batched.Batched with io.leangen.graphql.annotations.Batched (same @Batched, different package)
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());
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:
graphql.execution.batched.Batched
withio.leangen.graphql.annotations.Batched
(same@Batched
, different package)DataLoaderRegistry
either: a. usingGraphQLRuntime.newGraphQL
instead ofGraphQL.newGraphQL
b. manually, using the generatedBatchLoaderWithContext
instancesExamples:
a)
b)