GraphQL strawberry的使用回顾和体会

news/2025/2/14 3:19:43/

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.ppmy.cn/news/1039480.html

相关文章

2023-08-17力扣每日一题

链接: 1444. 切披萨的方案数 题意: 给定一个矩阵,其中含有多个苹果,需要切割k-1次,每次可以切割多行/多列,需要保证切割两个部分都有苹果,移除靠上/靠右的部分,对留下部分进行后续的切割&…

Linux 多进程

目录 0x01 linux中特殊的进程 0x02 进程的标识 0x03 创建子进程 0x01 linux中特殊的进程 0号进程:idle进程,系统启动加载的进程1号进程:systemd进程,系统初始化,是所有进程的祖先进程 init2号进程:kthre…

pytest自动化测试框架tep环境变量、fixtures、用例三者之间的关系

tep是一款测试工具,在pytest测试框架基础上集成了第三方包,提供项目脚手架,帮助以写Python代码方式,快速实现自动化项目落地。 在tep项目中,自动化测试用例都是放到tests目录下的,每个.py文件相互独立&…

Kotlin手写RxJava变换符

Kotlin手写RxJava变换符 本文链接,点击这里进入 1、核心点:中转站存储之前的数据 2、三行代码实现RxJava 使用create、map、observer fun main() {// create构造出RxJavaCore存放,lambda执行完的结果create{"WCH"}.map{ // 扩展…

Tenzing and Balls

题面翻译 有一个大小为 n n n 的数组 a a a。你可以进行下列操作任意多次&#xff1a; 选择 i i i 和 j j j&#xff0c;使 1 ≤ i < j ≤ ∣ a ∣ 1\leq i \lt j \leq |a| 1≤i<j≤∣a∣ 并且 a i a j a_ia_j ai​aj​&#xff0c; 从数组中删除 a i , a i 1 ,…

合宙Air724UG LuatOS-Air script lib API--wifiRil

wifiRil Table of Contents wifiRil wifiRil.regRsp(head, fnc, typ, formt) wifiRil.regUrc(prefix, handler) wifiRil.deRegUrc(prefix) wifiRil.request(cmd, arg, onrsp, delay, param) wifiRil 模块功能&#xff1a;esp8266 wifi模块AT命令交互管理 wifiRil.regRsp(head,…

龙迅LT9711 2PORT MIPI或者LVDS转TYPE-C

LT9711 1.描述&#xff1a; Lontium LT9711是双端口MIPI/LVDS到DP1.2转换器&#xff0c;内部有c型替代模式开关和PD控制器。MIPI DSI/CSI输入具有可配置的单端口或双端口&#xff0c;具有1个时钟通道&#xff0c;1个~4个数据通道&#xff0c;最大运行2Gbps/通道&#xff0c;可…

【Java开发实战攻关】「JPA技术专题」带你一同认识和使用JPA框架进行开发你的应用服务

带你一同认识和使用JPA框架进行开发你的应用服务 什么是JPA框架JPA持久层框架使用JPA实现持久化JPA注解介绍按类别划分的 JPA注解实体注解Entity模型注解Table示例-显示了如何使用此批注指定主表名。 注解TableGeneratorTableGenerator主要属性strategystrategy属性可以是下列枚…