Query based on data from multiple services (subgraphs)

Hi! We’re exploring breaking our monolith into smaller services. The federated graph concept so far looks really cool, with the entire concept of populating entities with data from different services, but now we’ve run into a challenge with how to query data based on criteria’s that’s spread across different services.To make it a bit more concrete, let’s take an example. We have two services providing two pieces of the puzzle to the federated graph:

1. The book service
This service holds a database with all books, and an id to it’s author. It’s schema looks something like this:

type Book @key(fields: "id") {
  id: Int!
  title: String!
  genre: String!
  author: Author!

type Author @key(fields: "id") {
  id: Int!

2. The author service
This service holds a database with all authors, and knows nothing about the books they’ve written. It’s schema looks something like this:

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

The backend teams are very happy with this solution with separation of concerns, each service is responsible for it’s own domain, and it’s easy for each team (owning a service each) to develop their individual subgraphs independent of each other.

The challenge
Now the frontend team comes asking for a query to be able to fetch all books. They want to be able to

  1. Filter all books by the origin country of the author
  2. Sort the books by the author’s name.

On a schema level this could be expressed the following way in the books service:

enum BooksOrderBy {

input BooksQueryInput {
  genres: [String!]
  authorCountries: [String!]
  orderBy: BooksOrderBy!

extend type Query {
  books(args: BooksQueryInput!): [Book!]!

But how can we make this work in practice when the books service knows nothing about the authors and vice versa? Is there some clever solution to this problem, or do we need to duplicate some data from the author service to the book service to be able to serve this request?For some more context:

  • Maybe obvious but this is a fictive example, but it includes the same challenges from our actual application.
  • In our application:
    • the book service holds ~50M books
    • the author service holds ~50K authors
  • For any given request, the amount of books being returned from the filter would be in the size of 10-1000 entities.

Thank you for your input :star2: