news 2026/6/16 8:47:50

Python GraphQL与graphene应用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python GraphQL与graphene应用

GraphQL查询语言。类型系统Schema定义。Query根查询。Mutation变更。Resolver解析器。graphene库实现。GraphQL与REST对比。动态数据获取。

import graphene

class User(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
age = graphene.Int()

class Query(graphene.ObjectType):
user = graphene.Field(User, id=graphene.ID(required=True))
users = graphene.List(User)

def resolve_user(root, info, id):
return User(id=id, name="Alice", age=30)

def resolve_users(root, info):
return [User(id=1, name="Alice", age=30),
User(id=2, name="Bob", age=25)]

class CreateUser(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
age = graphene.Int()
ok = graphene.Boolean()
user = graphene.Field(User)

def mutate(root, info, name, age=0):
return CreateUser(ok=True, user=User(id=3, name=name, age=age))

class Mutation(graphene.ObjectType):
create_user = CreateUser.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

result = schema.execute('''{
users { id name age }
}''')
print(result.data)

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/16 8:33:34

MoE模型真实激活率:拆解‘1.8万亿参数仅用2%’的工程真相

1. 项目概述:参数规模与稀疏激活的真相拆解“GPT-4有1.8万亿参数,但每次生成一个词只用其中2%”——这句话过去两年在技术社区反复刷屏,被当作大模型“聪明又高效”的铁证。可我第一次在内部技术分享会上听到这个说法时,下意识翻出…

作者头像 李华
网站建设 2026/6/16 8:26:34

魔兽世界插件开发终极解决方案:一站式API查询与宏命令管理平台

魔兽世界插件开发终极解决方案:一站式API查询与宏命令管理平台 【免费下载链接】wow_api Documents of wow API -- 魔兽世界API资料以及宏工具 项目地址: https://gitcode.com/gh_mirrors/wo/wow_api 你是否曾为寻找魔兽世界插件开发API而苦恼?是…

作者头像 李华
网站建设 2026/6/16 8:25:00

Windows系统深度优化与故障排查:从效率提升到稳定掌控的完整指南

1. 项目概述:不止是“小技巧”“Windows Tips and Tricks”这个标题,听起来像是那种随处可见的“10个你不知道的Windows技巧”合集。但如果你真的这么想,那就错过了它背后真正的价值。作为一个和Windows系统打了十几年交道的“老炮”&#xf…

作者头像 李华