当前位置: 首页 > news >正文

GraphQL strawberry的使用回顾和体会

GraphQL vs RESTful

简单来说GraphQL 比起 RESTful 集成额外一些功能

  1. 出入参校验、序列化 (简化后端编程)
  2. 自由可选的返回数据字段 (简化一些多余接口开发和沟通联调成本)

这些都是优点了。

开发效率在项目初期是很重要的,需要快速原型化。
但是后期稳定后,性能也很重要。

对比

RESTful + Pydantic

from sanic import Sanic, text
from pydantic import BaseModel
from typing import Listapp = Sanic("simple")class Simple(BaseModel):name: strage: inthobbies: List[str]@app.post("/rest")
async def rest_test(request):Simple.model_validate(request.json)return text("ok")

k6测试代码

import { check } from 'k6';
import http from 'k6/http';export default function () {let data = { "name": "Stephen Ling", "age": 28, "hobbies": ["coding", "coffee"] }const res = http.post('http://localhost:9090/rest', JSON.stringify(data), {headers: { 'Content-Type': 'application/json' },});check(res, {'is status 200': (r) => r.status === 200,});
}

在这里插入图片描述

GraphQL(strawberry)

k6测试代码

import { check } from 'k6';
import http from 'k6/http';export default function () {let data = {"query": "mutation {\n  resolveGraphql(name: \"Stephen Ling\", age: 28, hobbies: [\"coding\", \"coffee\"])\n}"}const res = http.post('http://localhost:9090/graphql', JSON.stringify(data), {headers: { 'Content-Type': 'application/json' },});check(res, {'is status 200': (r) => r.status === 200,});
}

默认情况

import strawberry
from strawberry.sanic.views import GraphQLView
from sanic import Sanic, textfrom pydantic import BaseModel
from typing import Listapp = Sanic("simple")@strawberry.type
class Mutation:@strawberry.mutationasync def resolve_graphql(self, name: str, age: int, hobbies: List[str]) -> str:return "ok"@strawberry.type
class Query:@strawberry.fieldasync def nothing(self) -> None:...app.add_route(GraphQLView.as_view(schema=strawberry.Schema(query=Query,mutation=Mutation,),),"/graphql",
)

在这里插入图片描述

加上缓存

...
from strawberry.extensions import ParserCache, ValidationCache...
app.add_route(GraphQLView.as_view(schema=strawberry.Schema(query=Query,mutation=Mutation,extensions=[ParserCache(), ValidationCache()],),),"/graphql",
)

在这里插入图片描述

体会

  1. graphql 适合减轻前后端联调的沟通成本。谁错谁对一目了然。
  2. 选择实现库之前,衡量一下性能代价是否能接受。
  3. 实现库之间尽可能横向对比一下。
  4. 在可以忍受的性能差距下,我会选择开发效率,毕竟每个代码的生命周期是有限的,没有必要死磕。
http://www.lryc.cn/news/133466.html

相关文章:

  • 08无监督学习——聚类
  • Python使用OpenCV库对彩色图像进行通道分离
  • 前端面试:【CSS】盒模型、选择器、布局、响应式设计、Flexbox 与 Grid
  • 深入浅出通过PHP封装根据商品ID获取抖音商品详情数据方法
  • 排序(七种排序)
  • 【工程优化问题】基于鲸鱼、萤火虫、灰狼优化算法的张力、压缩弹簧设计问题研究(Matlab代码实现)
  • sap ui5刷新页面的方式
  • Java课题笔记~ Fastjson 概述
  • Arduino 入门学习笔记11 读写内置EEPROM
  • 【Nginx】安装make后遇到/bin/sh: 第 0 行:cd: ../pcre-8.38: 没有那个文件或目录
  • 在Windows Server 2008上启用自动文件夹备份
  • 数据结构—线性表的查找
  • EndNote(一)【界面+功能介绍】
  • JWT令牌验证
  • 【微信小程序】下拉刷新功能实现
  • 三角函数与圆,角度和弧度 (草稿,建设中)
  • AIGC 施展“物理魔法”,3D视觉突破“精度极限”
  • redis 哨兵模式
  • java八股文面试[java基础]——String StringBuilder StringBuffer
  • [oneAPI] 基于BERT预训练模型的命名体识别任务
  • SSL证书如何使用?SSL保障通信安全
  • postgresql 的递归查询
  • Go语言进阶:函数、指针、错误处理
  • 最强自动化测试框架Playwright(30)-JS句柄
  • Ctfshow web入门 命令执行RCE篇 web29-web77 与 web118-web124 详细题解 全
  • 【C++ STL之map,set,pair详解】
  • Python LEGB规则解析与应用
  • 气象监测站:用科技感知气象变化
  • Linux debian12解压和压缩.rar文件教程
  • 探析国际大文件传输的花费与降低开销的小妙招