當創建web服務應用程序時,可以選擇使用REST或GraphQL作為通信模式。兩者都可能在HTTP上使用JSON,但有不同的優點和缺點。
(資料圖片)
本文主要比較GraphQL和REST,以操作一個產品數據庫示例,比較兩種解決方案在執行相同的客戶端操作時的差異:
創建處于草稿狀態的產品更新產品詳細信息獲取產品列表獲取單個產品及其訂單的詳細信息RESTREST(Representational State Transfer,代表性狀態傳輸)的主要數據元素稱為Resource。在本例中,資源是“產品”。
創建產品curl --request POST "http://localhost:8081/product" \--header "Content-Type: application/json" \--data "{ "name": "Watch", "description": "Special Swiss Watch", "status": "Draft", "currency": "USD", "price": null, "imageUrls": null, "videoUrls": null, "stock": null, "averageRating": null}"更新產品
curl --request PUT "http://localhost:8081/product/{product-id}" \--header "Content-Type: application/json" \--data "{ "name": "Watch", "description": "Special Swiss Watch", "status": "Draft", "currency": "USD", "price": 1200.0, "imageUrls": [ "https://graphqlvsrest.com/imageurl/product-id" ], "videoUrls": [ "https://graphqlvsrest.com/videourl/product-id" ], "stock": 10, "averageRating": 0.0}"獲取產品列表
curl --request GET "http://localhost:8081/product?size=10&page=0"
{ "id": 1, "name": "T-Shirt", "description": "Special beach T-Shirt", "status": Published, "currency": "USD", "price": 30.0, "imageUrls": ["https://graphqlvsrest.com/imageurl/1"], "videoUrls": ["https://graphqlvsrest.com/videourl/1"], "stock": 10, "averageRating": 3.5 }通過訂單獲取單個產品
要獲取產品及其訂單,通常需要先調用產品列表API,然后調用訂單資源以查找相關訂單:
curl --request GET "localhost:8081/order?product-id=1"
{ "id": 1, "productId": 1, "customerId": "de68a771-2fcc-4e6b-a05d-e30a8dd0d756", "status": "Delivered", "address": "43-F 12th Street", "creationDate": "Mon Jan 17 01:00:18 GST 2022"}
除了獲取所有產品的原始操作外,還需要對每個感興趣的產品執行一次此操作,這會產生N+1的相關問題。
GraphQLGraphQL API操作包含Queries和Mutations。Queries負責獲取數據,Mutations用于創建和更新。
Queries和Mutations的Schema模式定義了客戶端可能的請求和響應。
創建產品curl --request POST "http://localhost:8081/graphql" \--header "Content-Type: application/json" \--data \"{ "query": "mutation {saveProduct ( product: { name: \"Bed-Side Lamp\", price: 24.0, status: \"Draft\", currency: \"USD\" }){ id name currency price status} }"}"
{ "data": { "saveProduct": { "id": "12", "name": "Bed-Side Lamp", "currency": "USD", "price": 24.0, "status": "Draft" } }}更新產品
curl --request POST "http://localhost:8081/graphql" \--header "Content-Type: application/json" \--data \"{"query": "mutation {updateProduct( id: 11 product: { price: 14.0, status: \"Publish\" }){ id name currency price status } }","variables":{}}"
{ "data": { "updateProduct": { "id": "12", "name": "Bed-Side Lamp", "currency": "USD", "price": 14.0, "status": "Published" } }}獲取產品列表
curl --request POST "http://localhost:8081/graphql" \--header "Content-Type: application/json" \--data \"{ "query": "query {products(size:10,page:0){id name status}}"}"
{ "data": { "products": [ { "id": "1", "name": "T-Shirt", "status": "Published" }, ... ] }}通過訂單獲取單個產品
curl --request POST "http://localhost:8081/graphql" \--header "Content-Type: application/json" \--data \"{ "query": "query {product(id:1){ id name orders{customerId address status creationDate}}}"}"
{ "data": { "product": { "id": "1", "name": "T-Shirt", "orders": [ { "customerId": "de68a771-2fcc-4e6b-a05d-e30a8dd0d756", "status": "Delivered", "address": "43-F 12th Street", "creationDate": "Mon Jan 17 01:00:18 GST 2022" }, ... ] } }}GraphQL優勢
GraphQL允許靈活和動態的查詢:
客戶端只能請求Schema已定義的字段支持別名用于請求具有自定義鍵值的字段客戶端可以使用查詢來管理返回結果的順序客戶端可以更好地與API中的任何更改解耦GraphQL傾向于避免昂貴的操作,通常可以使用GraphQL在一個請求中獲取所需的所有數據。
何時使用RESTGraphQL不能替代REST。在以下情況下,可能更適合使用REST:
應用程序是資源驅動的,其中的操作與各個資源實體非常直接和完全地聯系在一起需要web緩存,因為GraphQL本身并不支持需要文件上傳,因為GraphQL本身并不支持結論選擇使用REST或GraphQL作為通信模式,需要由業務場景決定。GraphQL靈活性也決定了其一定程度上的復雜性。
使用GraphQL也需要考慮在應用層面的緩存優化,和解決N+1問題的批量操作優化。
關鍵詞:
X 關閉
X 關閉
- 15G資費不大降!三大運營商誰提供的5G網速最快?中國信通院給出答案
- 2聯想拯救者Y70發布最新預告:售價2970元起 迄今最便宜的驍龍8+旗艦
- 3亞馬遜開始大規模推廣掌紋支付技術 顧客可使用“揮手付”結賬
- 4現代和起亞上半年出口20萬輛新能源汽車同比增長30.6%
- 5如何讓居民5分鐘使用到各種設施?沙特“線性城市”來了
- 6AMD實現連續8個季度的增長 季度營收首次突破60億美元利潤更是翻倍
- 7轉轉集團發布2022年二季度手機行情報告:二手市場“飄香”
- 8充電寶100Wh等于多少毫安?鐵路旅客禁止、限制攜帶和托運物品目錄
- 9好消息!京東與騰訊續簽三年戰略合作協議 加強技術創新與供應鏈服務
- 10名創優品擬通過香港IPO全球發售4100萬股 全球發售所得款項有什么用處?
