亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

 

概述

當創建web服務應用程序時,可以選擇使用REST或GraphQL作為通信模式。兩者都可能在HTTP上使用JSON,但有不同的優點和缺點。

本文主要比較GraphQL和REST,以操作一個產品數據庫示例,比較兩種解決方案在執行相同的客戶端操作時的差異:

  • 創建處于草稿狀態的產品
  • 更新產品詳細信息
  • 獲取產品列表
  • 獲取單個產品及其訂單的詳細信息

REST

REST(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的相關問題。

GraphQL

GraphQL 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在一個請求中獲取所需的所有數據。

何時使用REST

GraphQL不能替代REST。在以下情況下,可能更適合使用REST:

  • 應用程序是資源驅動的,其中的操作與各個資源實體非常直接和完全地聯系在一起
  • 需要web緩存,因為GraphQL本身并不支持
  • 需要文件上傳,因為GraphQL本身并不支持

結論

選擇使用REST或GraphQL作為通信模式,需要由業務場景決定。GraphQL靈活性也決定了其一定程度上的復雜性。

使用GraphQL也需要考慮在應用層面的緩存優化,和解決N+1問題的批量操作優化。

分享到:
標簽:GraphQL
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定