Hey, thanks for your question!
These are the main advantages of using batching over “join”:
- Not all databases support “joins”. For example, we also use MongoDB which doesn’t support this feature (although it’s possible to use Aggregation Pipeline, it’s more like a “left join” with some limitations). In complex scenarios we should “join” collections manually anyway.
- Using “join” usually means that we’re preloading data and passing it down whether implicitly (e.g. ORM like ActiveRecord with
eager_load) or explicitly through variables. It may be a problem if we need to pass data several layers below. For example, load all Comments and pass them to Posts through Users.
- By preloading data in advance, we’re coupling our code. For example, while loading Users, we have to know that we also need to load all Comments for the User Posts. With batching we’re able to isolate this logic, so we’re describing how to load Comments in a batch right in a place where we need the Comments.
- With GraphQL using “joins” is not so flexible. Since users can ask for any allowed relations described in the schema. So our code has to be smart to preload multiple nested relations, but only if a user requested them. Since batching is isolated, it’ll make requests to load data in a batch only if we really need it.
- Batching is a general technique which can be used not only for SQL database queries. It can be used to avoid N+1 HTTP requests as well.