Fetch Multiple Related Tables in One API Call Using OData $expand in CData API Server

Pulling a customer, their orders, and the products in each order often takes three or four separate API calls per screen. The result is slower load times and client-side code that’s hard to maintain. With CData API Server, you define a relationship once and use the OData $expand keyword to return a parent row and its related child rows in a single request.

In this article, you’ll learn how to:

  • Define one-to-many and many-to-one relationships on columns in API Server
  • Use $expand to return related tables in one call
  • Combine $expand with $select and $filter to control which columns and rows come back
  • Use pre-joined SQL views when your data needs three or more levels of depth

To learn more, read:

Fetch Multiple Related Tables in One API Call Using OData $expand in CData API Server

One practical caveat worth adding: $expand on a one-to-many relationship can inflate the response quickly, so pairing it with $select on the child columns and a $filter or $top on the children is often necessary rather than optional. Also, nesting three or more levels tends to generate expensive queries, which is where the pre-joined view approach earns its keep. If a screen only needs a couple of child fields, it is worth checking the SQL that gets generated to confirm it is not pulling every column anyway.