Merging Data Loaders in a Federated Graph Using Spring Boot

Hi there :wave:. I’m working on implementing a batch loader in a federated graph using Spring Boot. I’m exploring consolidating the loading process for scoring and bestseller attributes in the review subgraph.

In kotlin-graphql, this could be solved using a FederatedTypeResolver, but I’m interested in understanding how to achieve the same outcome in my current setup.

Here’s the schema I’m working with:

Product subgraph:

type Query {
    product(id: ID!): Product
    products: [Product!]!
}

type Product @key(fields: "id") {
    id: ID!
    name: String!
    description: String
}

Review subgraph:

type Product @key(fields: "id") @extends {
    id: ID! @external
    reviews: [Review!]!
    scoring: Float!
    bestSeller: Boolean!
}

type Review {
    id: ID!,
    text: String
    starRating: Int!
}

I’ve implemented batch loading for both the scoring and bestSeller fields with the following code:

  @BatchMapping(typeName = "Product", field = "scoring")
  public Mono<Map<Product, Float>> scoring(List<Product> products) {
    return Mono.just(products.stream().collect(Collectors.toMap(product -> product, product -> new Random().nextFloat())));
  }

  @BatchMapping(typeName = "Product", field = "bestSeller")
  public Mono<Map<Product, Boolean>> bestSeller(List<Product> products) {
    return Mono.just(products.stream().collect(Collectors.toMap(product -> product, product -> new Random().nextBoolean())));
  }

My question is: Is it possible to merge these two data loaders and then use the consolidated representation to create a single resolver that can handle both the scoring and bestSeller fields?