偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

GraphQL對比Rest,你學到了什么?

網(wǎng)絡(luò) 通信技術(shù)
選擇使用REST或GraphQL作為通信模式,需要由業(yè)務場景決定。GraphQL靈活性也決定了其一定程度上的復雜性。使用GraphQL也需要考慮在應用層面的緩存優(yōu)化,和解決N+1問題的批量操作優(yōu)化。

概述

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

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

  • 創(chuàng)建處于草稿狀態(tài)的產(chǎn)品
  • 更新產(chǎn)品詳細信息
  • 獲取產(chǎn)品列表
  • 獲取單個產(chǎn)品及其訂單的詳細信息

REST

REST(Representational State Transfer,代表性狀態(tài)傳輸)的主要數(shù)據(jù)元素稱為Resource。在本例中,資源是“產(chǎn)品”。

  • 創(chuàng)建產(chǎn)品
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
}'
  • 更新產(chǎn)品
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
}'
  • 獲取產(chǎn)品列表
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
}
  • 通過訂單獲取單個產(chǎn)品

要獲取產(chǎn)品及其訂單,通常需要先調(diào)用產(chǎn)品列表API,然后調(diào)用訂單資源以查找相關(guān)訂單:

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"
}

除了獲取所有產(chǎn)品的原始操作外,還需要對每個感興趣的產(chǎn)品執(zhí)行一次此操作,這會產(chǎn)生N+1的相關(guān)問題。

GraphQL

GraphQL API操作包含Queries和Mutations。Queries負責獲取數(shù)據(jù),Mutations用于創(chuàng)建和更新。

Queries和Mutations的Schema模式定義了客戶端可能的請求和響應。

  • 創(chuàng)建產(chǎn)品
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"
}
}
}
  • 更新產(chǎn)品
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"
}
}
}
  • 獲取產(chǎn)品列表
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"
},
...
]
}
}
  • 通過訂單獲取單個產(chǎn)品
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優(yōu)勢

GraphQL允許靈活和動態(tài)的查詢:

  • 客戶端只能請求Schema已定義的字段
  • 支持別名用于請求具有自定義鍵值的字段
  • 客戶端可以使用查詢來管理返回結(jié)果的順序
  • 客戶端可以更好地與API中的任何更改解耦

GraphQL傾向于避免昂貴的操作,通??梢允褂肎raphQL在一個請求中獲取所需的所有數(shù)據(jù)。

何時使用REST

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

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

結(jié)論

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

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


責任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2025-02-28 00:03:00

2023-10-16 08:55:43

Redisson分布式

2024-11-13 09:22:40

2022-07-19 08:04:04

HTTP應用層協(xié)議

2023-06-03 00:05:18

TypeScriptJSDoc掃描器

2024-07-31 09:28:56

2024-10-18 11:48:00

2024-04-12 08:54:13

從庫數(shù)據(jù)庫應用

2024-08-12 15:44:06

2023-06-06 08:14:18

核心Docker應用程序

2023-04-26 22:52:19

視覺人臉檢測人臉對齊

2021-03-09 09:55:02

Vuejs前端代碼

2021-04-23 09:09:19

GraphQLREST查詢

2021-09-03 06:46:34

MyBatis緩存后端

2023-04-26 01:25:05

案例故障模型

2023-11-09 09:13:48

GraphQLAPI 架構(gòu)

2021-12-26 18:30:56

嵌入式ARM鏈接

2023-06-30 07:30:38

2021-07-29 18:46:52

可視化類型圖形化

2024-04-16 12:00:14

API系統(tǒng)
點贊
收藏

51CTO技術(shù)棧公眾號