From 35407fcd1ee375f12f48e19716dedc2bcc991521 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Tue, 17 Aug 2021 15:21:11 +0200 Subject: [PATCH] GraphQL: added example of VertxBatchLoader to the doc Closes #1981 Signed-off-by: Thomas Segismont --- vertx-web-graphql/src/main/asciidoc/index.adoc | 10 +++++++--- .../src/main/java/examples/GraphQLExamples.java | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/vertx-web-graphql/src/main/asciidoc/index.adoc b/vertx-web-graphql/src/main/asciidoc/index.adoc index 0035e4b541..85def5ad02 100644 --- a/vertx-web-graphql/src/main/asciidoc/index.adoc +++ b/vertx-web-graphql/src/main/asciidoc/index.adoc @@ -199,9 +199,6 @@ First, create a batch loader: {@link examples.GraphQLExamples#createBatchLoader} ---- -TIP: If you work with Vert.x callback-based APIs, you may use a {@link io.vertx.ext.web.handler.graphql.dataloader.VertxBatchLoader} -or a {@link io.vertx.ext.web.handler.graphql.dataloader.VertxMappedBatchLoader} to simplify your code. - Then, configure the {@link io.vertx.ext.web.handler.graphql.GraphQLHandler} to create a `DataLoaderRegistry` for each request: [source,$lang] @@ -209,6 +206,13 @@ Then, configure the {@link io.vertx.ext.web.handler.graphql.GraphQLHandler} to c {@link examples.GraphQLExamples#dataLoaderRegistry} ---- +If you work with Vert.x APIs, you may use a {@link io.vertx.ext.web.handler.graphql.dataloader.VertxBatchLoader} or a {@link io.vertx.ext.web.handler.graphql.dataloader.VertxMappedBatchLoader} to simplify your code: + +[source,$lang] +---- +{@link examples.GraphQLExamples#createVertxBatchLoader} +---- + === File uploads https://github.com/jaydenseric/graphql-multipart-request-spec[GraphQL multipart request] is an interoperable multipart form field structure for `GraphQL` requests. diff --git a/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java b/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java index 8443f0c1ff..791f57a50b 100644 --- a/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java +++ b/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java @@ -29,6 +29,7 @@ import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.BodyHandler; import io.vertx.ext.web.handler.graphql.*; +import io.vertx.ext.web.handler.graphql.dataloader.VertxBatchLoader; import io.vertx.ext.web.handler.graphql.schema.VertxDataFetcher; import io.vertx.ext.web.handler.graphql.schema.VertxPropertyDataFetcher; import org.dataloader.*; @@ -213,7 +214,10 @@ public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environm } public void createBatchLoader() { - BatchLoaderWithContext linksBatchLoader = this::retrieveLinksFromBackend; + BatchLoaderWithContext linksBatchLoader = (ids, env) -> { + // retrieveLinksFromBackend takes a list of ids and returns a CompletionStage for a list of links + return retrieveLinksFromBackend(ids, env); + }; } private CompletionStage> retrieveLinksFromBackend(List ids, BatchLoaderEnvironment environment) { @@ -230,6 +234,17 @@ public void dataLoaderRegistry(GraphQL graphQL, BatchLoaderWithContext commentsBatchLoader = VertxBatchLoader.create((ids, env) -> { + // findComments takes a list of ids and returns a Future for a list of links + return findComments(ids, env); + }); + } + + private Future> findComments(List ids, BatchLoaderEnvironment env) { + return null; + } + public void addApolloWsHandlerToRouter(Router router) { GraphQL graphQL = setupGraphQLJava();