Skip to content

Commit

Permalink
GraphQL: added example of VertxBatchLoader to the doc
Browse files Browse the repository at this point in the history
Closes vert-x3#1981

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Aug 17, 2021
1 parent ebfa510 commit 35407fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 7 additions & 3 deletions vertx-web-graphql/src/main/asciidoc/index.adoc
Expand Up @@ -199,16 +199,20 @@ 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]
----
{@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.
Expand Down
17 changes: 16 additions & 1 deletion vertx-web-graphql/src/main/java/examples/GraphQLExamples.java
Expand Up @@ -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.*;
Expand Down Expand Up @@ -213,7 +214,10 @@ public DataFetcher<Object> getDefaultDataFetcher(FieldWiringEnvironment environm
}

public void createBatchLoader() {
BatchLoaderWithContext<String, Link> linksBatchLoader = this::retrieveLinksFromBackend;
BatchLoaderWithContext<String, Link> linksBatchLoader = (ids, env) -> {
// retrieveLinksFromBackend takes a list of ids and returns a CompletionStage for a list of links
return retrieveLinksFromBackend(ids, env);
};
}

private CompletionStage<List<Link>> retrieveLinksFromBackend(List<String> ids, BatchLoaderEnvironment environment) {
Expand All @@ -230,6 +234,17 @@ public void dataLoaderRegistry(GraphQL graphQL, BatchLoaderWithContext<String, L
});
}

public void createVertxBatchLoader() {
BatchLoaderWithContext<Long, String> 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<List<String>> findComments(List<Long> ids, BatchLoaderEnvironment env) {
return null;
}

public void addApolloWsHandlerToRouter(Router router) {
GraphQL graphQL = setupGraphQLJava();

Expand Down

0 comments on commit 35407fc

Please sign in to comment.