したがって、RESTful API の一般的なパターンは、関連するオブジェクトを取得するために使用できるリンクが埋め込まれた単一のオブジェクトを返すことです。しかし、便宜上、オブジェクト グラフのチャンク全体を一度に引き戻したい場合があります。
たとえば、customers、orders、およびreturnsを持つ店舗アプリケーションがあるとします。顧客 ID 12345 の個人情報、すべての注文、およびすべての返品を一緒に表示したいと考えています (おそらく、常に注文を返品し、顧客の個人情報を返品するとは限りません)。
これを行う純粋な RESTful な方法は、次のようなものです。
GET /
- 顧客を照会するものを含む、リンク テンプレートのリストを返します
GET /customers/12345
( のリンク テンプレートに基づく/
)- お客様の個人情報を返します
- この顧客の注文と返品を取得するリンクを返します
GET /orders?customerId=12345
(/customers/12345
回答より)- 顧客 12345 の注文を取得します
GET /returns?customerId=12345
(/customers/12345
回答より)- 顧客 12345 の返品を取得します
customers
しかし、 URI を取得したら、これをすべて 1 つのクエリで取得できると便利です。複数のリクエストを作成する代わりに、一部またはすべてのリンクをトランスクルージョンしたい、この種の便利なクエリのベスト プラクティスはありますか? 私は次のようなことを考えています:
GET /customers/12345?include=orders,returns
しかし、人々がこれを行っている方法がある場合、私は単に何かをでっち上げたくはありません。
(FWIW、私はストアを構築していないので、これらがモデルに適したオブジェクトであるかどうか、または実際の製品にどのようにドリルダウンするかなどについて、口論しないでください。)
追加の更新: HALでは、これらは「埋め込みリソース」と呼ばれるように見えますが、示されている例では、埋め込むリソースを選択する方法がないようです。クエリ パラメータとして使用して、上記のような内容を示唆するブログ投稿を見つけました。embed
GET /ticket/12?embed=customer.name,assigned_user
これは標準的または準標準的な慣行ですか、それとも 1 人のブロガーがでっち上げたものですか?