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

Add Vertx Future, RxJava3 API to Vertx DataLoader Helper #1981

Closed
hantsy opened this issue Jun 20, 2021 · 1 comment
Closed

Add Vertx Future, RxJava3 API to Vertx DataLoader Helper #1981

hantsy opened this issue Jun 20, 2021 · 1 comment
Assignees
Labels
documentation enhancement graphql Issues related to GraphQL module
Milestone

Comments

@hantsy
Copy link

hantsy commented Jun 20, 2021

There are several helpers in Vertx 4.1.0 to build GraphQL DataLoader / BatchLoaders.

public class AuthorsDataLoader implements VertxBatchLoader<String, Author> {
    final AuthorService authorService;
    @Override
    public CompletionStage<List<Author>> load(List<String> keys, BatchLoaderEnvironment environment) {
        return null;
    }
}

But there is no helpers to use Future or Flowable when building a DataLoader.

Hope there are some interfaces to use Future or Flowable directly.

public class AuthorsDataLoader implements VertxFavoredBatchLoader<String, Author> {
    final AuthorService authorService;
    @Override
    public Future<List<Author>> load(List<String> keys, BatchLoaderEnvironment environment) {
        return null;
    }
}

or

public class AuthorsDataLoader implements VertxFavoredBatchLoader<String, Author> {
    final AuthorService authorService;
    @Override
    public Flowable<List<Author>> load(List<String> keys, BatchLoaderEnvironment environment) {
        return null;
    }
}
@vietj vietj added this to the 4.2.0 milestone Jun 21, 2021
@tsegismont tsegismont added the graphql Issues related to GraphQL module label Jun 22, 2021
@tsegismont tsegismont self-assigned this Jun 22, 2021
@tsegismont
Copy link
Contributor

In your example, AuthorsDataLoader implements VertxBatchLoader but it shouldn't.

Vert.x Web GraphQL already has helpers for futurized API.

Consider a GraphQL server that exposes blog posts data. The server can retrieve all blog posts along with their comments.

To avoid sending a query fetching comments for each post, you could create a DataLoader:

DataLoader<Integer, JsonArray> commentDataLoader = DataLoader.newMappedDataLoader(
  VertxMappedBatchLoader.create((postIds, batchLoaderEnvironment) -> findComments(postIds, batchLoaderEnvironment))
);

The findComments method returns a Future:

private Future<Map<Integer, JsonArray>> findComments(Set<Integer> postIds, BatchLoaderEnvironment env) {
  Collector<Row, ?, Map<Integer, JsonArray>> collector = groupingBy(
    row -> row.getInteger("post_id"),
    mapping(this::toComment, collectingAndThen(toList(), JsonArray::new))
  );
  return pgClient.preparedQuery("select * from comments where post_id = any($1)")
    .collecting(collector)
    .execute(Tuple.of(postIds.toArray(new Integer[0])))
    .map(SqlResult::value);
}

You can find the code here.

Concerning the RxJava integration, you may use the RxJavaJdk8Interop libraray as explained in the doc.

So I think what we could add some documentation about VertxBatchLoader to avoid confusions.

@tsegismont tsegismont modified the milestones: 4.2.0, 4.1.3 Aug 17, 2021
tsegismont added a commit that referenced this issue Aug 17, 2021
Closes #1981

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation enhancement graphql Issues related to GraphQL module
Development

No branches or pull requests

3 participants