How to combine data for a field from multiple subgraph in apollo federation using java?


I have this project requirement where I need the data coming from multiple subgraph to be combined as one in the final query.
Below is my use-case

type book {
id : ID!
bookname : String
author : [Author]

type author{
authorId : ID
authorname : String

So, I want this author details coming from multiple subgraphs i.e author1, author2… so on. And finally the result will be a combination of all the authors data from multiple subgraphs.

Can anyone pl help with this?

Hello :wave:

Federation allows you to build supergraph in a distributed manner where different parts of the graph are subject matter experts (SME) on given functionality. In your case you might have Author subgraph and Book subgraph. You could then extend the Book type in your Author subgraph populate the author field, e.g.

type Author @key(fields: "id") {
  id: ID!
  name: String!

type Book @key(fields: "id") {
  id: ID!
  author: [Author]

Federation does not solve the problem of fetching the downstream data from multiple data sources, i.e. if your author information is coming from service A, B or C then Author subgraph should abstract this information so clients know that they can fetch authors but they don’t know anything about the underlying services A, B and C.

See Federation docs for more details.

Thanks @dkuc for replying
But I was actually looking for a scenario where I have a common data object which is getting stored in both services A & B. Can the federation collects data from both the services and produce the result which is a merger of both?



You can have a resolver across many subgraphs for the same @provides field but that would only be used for optimization purposes. Federation will not query for the same field across multiple subgraphs.