All Questions
Api Design medium

A GraphQL query fetches 50 users and their posts. The resolver fetches users (1 query), then for each user fetches posts (50 queries). How is this solved?

  1. A Implement the DataLoader pattern to batch IDs from the event loop tick and fetch all related posts in a single 'WHERE IN' database query.
  2. B Configure the GraphQL server to use deep-fragment caching, storing the result of every individual post resolver to avoid redundant database calls.
  3. C Modify the schema to include a 'post_list' field on the User type that performs a manual SQL join at the database layer during the user fetch.
  4. D Shift the responsibility to the client application to fetch users first, and then make 50 individual parallel REST calls to fetch posts per user.

One system design question a day, with spaced repetition so the ones you miss come back.

Get the app

Related questions