fullstack GraphQL学习笔记(15)错误处理
1、schema errors
作为具有强类型系统的语言,GraphQL可以预先确定查询是否有效。查询和突变的所有字段都具有强类型,因此请求和输入错误数据将产生错误。
2、Graphene Errors
使用GraphQLError
# ...code
# Add after the imports
from graphql import GraphQLError
# ...code
class CreateVote(graphene.Mutation):
user = graphene.Field(UserType)
link = graphene.Field(LinkType)
class Arguments:
link_id = graphene.Int()
def mutate(self, info, link_id):
user = info.context.user
if user.is_anonymous:
#1
raise GraphQLError('You must be logged to vote!')
link = Link.objects.filter(id=link_id).first()
if not link:
#2
raise Exception('Invalid Link!')
Vote.objects.create(
user=user,
link=link,
)
return CreateVote(user=user, link=link)
#1和#2代码引发异常 - 使用两个不同的异常类 - 但给出相同的结果。
浙公网安备 33010602011771号