您好,登錄后才能下訂單哦!
一種用于 API 的查詢語言
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)),
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
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。