GraphQLgraphene-django基本使用文档-创新互联
graphene-django 基本使用文档
介绍
一种用于 API 的查询语言
成都创新互联是一家专注于网站制作、成都网站设计与策划设计,龙岩网站建设哪家好?成都创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:龙岩等地区。龙岩做网站价格咨询:18982081108
GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。
文档
官网 http://graphql.cn/
参考文档 https://passwo.gitbook.io/graphql/index/drf
个人项目应用
https://github.com/hequan2017/seal
模块
pip install graphene-django
使用
INSTALLED_APPS = [
'graphene_django',
]
GRAPHENE = {
'SCHEMA': 'app.schema.schema'
}
urls.py
from graphene_django.views import GraphQLView
from app.schema import schema
path('graphql/', GraphQLView.as_view(graphiql=True, schema=schema)),
app/schema.py
from django.contrib.auth.models import User as Users
from graphene_django import DjangoObjectType
import graphene
# 相关文档 https://passwo.gitbook.io/graphql/index/drf
class UserType(DjangoObjectType):
class Meta:
model = Users
class Query(graphene.ObjectType):
users = graphene.List(UserType)
# List == Field:
# List 返回结果会是遍历所有查询结果
# Field 返回结果只存在单个 (其中可添加参数, ex. pk)
single_user = graphene.Field(UserType, pk=graphene.Int())
# 定义函数名的格式: resolve_字段
# **kwargs 传递参数
# pk: 如果在字段中定义, 则方法参数中必含
def resolve_users(self, info, **kwargs):
return Users.objects.all()
def resolve_single_user(self, info, pk):
return Users.objects.get(id=pk)
class TQuery(Query, graphene.ObjectType):
pass
class CreateUser(graphene.Mutation):
class Arguments:
username = graphene.String(required=True)
info = graphene.Field(UserType)
ok = graphene.Boolean()
def mutate(self, info, **kwargs):
# print(info.context.user, '==当前用户==')
# kwargs 是传递参数中的变量
# user = info.context.user
user_obj = Users(**kwargs)
try:
user_obj.save()
ok = True
except Exception as e:
print(e)
ok = False
return CreateUser(ok=ok, info=user_obj)
class CMutation(object):
create_user = CreateUser.Field()
class UpdateUser(graphene.Mutation):
class Arguments:
username = graphene.String()
pk = graphene.Int(required=True)
info = graphene.Field(UserType)
ok = graphene.Boolean()
def mutate(self, info, **kwargs):
pk = kwargs.get('pk')
user_obj = Users.objects.get(id=pk)
if not user_obj:
return UpdateUser(ok=False)
user_obj.__dict__.update(**kwargs)
user_obj.save()
ok = True
return UpdateUser(ok=ok, info=user_obj)
class UMutation(object):
update_user = UpdateUser.Field()
class DeleteUser(graphene.Mutation):
class Arguments:
pk = graphene.Int()
ok = graphene.Boolean()
def mutate(self, info, **kwargs):
pk = kwargs.get('pk')
user = Users.objects.get(id=pk)
user.delete()
return DeleteUser(ok=True)
class DMutation(object):
delete_user = DeleteUser.Field()
class Mutations(CMutation, UMutation,DMutation,graphene.ObjectType):
pass
schema = graphene.Schema(query=TQuery, mutation=Mutations)
请求
请求地址 : http://localhost/graphql
GraphQL 请求参数
query{
users{
id,
username,
email
}
}
query{
singleUser(pk: 1){
username,
email
}
}
mutation createUser {
createUser (username: "test1") {
info {
id,
},
ok
}
}
mutation updateUser {
updateUser (pk:2,username: "test2") {
info {
id,
},
ok
}
}
mutation deleteUser {
deleteUser (pk:2) {
ok
}
}
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
当前题目:GraphQLgraphene-django基本使用文档-创新互联
分享路径:http://ybzwz.com/article/jphij.html