投影可幫助您僅返回客戶實際需要的內容。 較小的承載可改善效能、降低網路成本,並減少用戶端剖析額外負荷。 資料 API 產生器 (DAB) 會透過查詢參數實 $select 作 REST 的投影。
備註
REST 查詢參數名稱 (包括 $select) 區分大小寫。 欄位名稱也會根據您設定或公開的內容區分大小寫。
移至 本檔案的GraphQL版本。
基本選擇
樣式
GET /api/{entity}?$select=FieldA,FieldB,FieldC
如果省略,DAB $select 會傳回呼叫者角色有權讀取的所有欄位 (受 include 限於 和 exclude 配置及欄位層級許可權)。 沒有萬用字元權杖,例如 *;省略是您 $select 要求完整允許的圖形的方式。
範例
# Return all accessible fields
GET /api/author
# Return only first_name
GET /api/author?$select=first_name
# Return only first_name and last_name
GET /api/author?$select=first_name,last_name
內部與回應資料行
您不需要投影主索引鍵或排序欄位。 如果省略,它們不會出現在 JSON 回應中。 但是,DAB 可能會在內部獲取強制執行安全策略(行級過濾器、字段遮罩)和處理分頁游標($after / nextLink)所需的額外列。
備註
除非您明確要求,否則會在回應之前移除這些內部擷取的資料行。
Example
GET /api/book?$select=id,title&$orderby=publisher_id desc&$first=5
概念性 SQL
SELECT TOP (6) -- first (5) + 1 probe row for paging
[b].[id],
[b].[sku_title] AS title
FROM dbo.books AS [b]
ORDER BY [b].[publisher_id] DESC, [b].[id] ASC;
回應
{
"value": [
{ "id": 101, "title": "Example 1" },
{ "id": 77, "title": "Example 2" },
{ "id": 42, "title": "Example 3" },
{ "id": 33, "title": "Example 4" },
{ "id": 5, "title": "Example 5" }
],
"nextLink": "..."
}
進一步瞭解 分頁和 after 關鍵字。
額外的內部資料行和第六個探針列在承載中看不到。
預存程序
對於預存程序支援的實體, $select 不會解譯為投影子句。 相反地,查詢字串索引鍵/值組 (辨識的系統參數除外,例如 $filter、 $orderby等) 會被視為預存程序參數。
$select 沒有效果;程序的結果集會定義形狀。
範例設定
{
"runtime": {
"pagination": {
"default-page-size": 100,
"max-page-size": 100000
}
},
"entities": {
"Book": {
"source": {
"type": "table",
"object": "dbo.books"
},
"mappings": {
"sku_title": "title",
"sku_price": "price"
},
"relationships": {
"book_category": {
"cardinality": "one",
"target.entity": "Category",
"source.fields": [ "category_id" ],
"target.fields": [ "id" ]
}
}
},
"Category": {
"source": {
"type": "table",
"object": "dbo.categories"
},
"relationships": {
"category_books": {
"cardinality": "many",
"target.entity": "Book",
"source.fields": [ "id" ],
"target.fields": [ "category_id" ]
}
}
}
}
}
另請參閱
| 概念 | REST | GraphQL | 目標 |
|---|---|---|---|
| Projection | $select | 項目 | 選擇要傳回的欄位 |
| 篩選 | $filter | 篩選 | 依條件限制資料列 |
| 排序 | $orderby | orderBy | 定義排序順序 |
| 頁面大小 | $first | first | 限制每頁的項目數 |
| 繼續 | $after | 後 | 使用游標從最後一頁繼續 |
備註
REST 關鍵字會遵循 OData 慣例,以 開 $頭。