我正在尝试组织我的突变,以使它们不全都位于顶层。这是我要放在“会话”部分下的一系列突变:
import graphene
from django.contrib.auth import authenticate
from rest_framework.authtoken.models import Token
class Login(graphene.Mutation):
token = graphene.String()
username = graphene.String()
user_id = graphene.Int()
class Arguments:
username = graphene.String(required=True)
password = graphene.String(required=True)
def mutate(self, info, **kwargs):
username = kwargs.get('username', '').strip()
password = kwargs.get('password', '').strip()
if username and password:
# Test username/password combination
user = authenticate(username=username, password=password)
# Found a match
if user is not None and user.is_active:
token = Token.objects.get_or_create(user=user)[0]
# return HttpResponse(token.key, content_type='text/plain')
return Login(token=token.key, username=user.username, user_id=user.id)
# credential check failed
return Login()
class Logout(graphene.Mutation):
success = graphene.Boolean()
def mutate(self, info):
if info.context.user is not None:
info.context.user.token.delete()
return Logout(success=True)
class SessionsMutations(graphene.ObjectType):
login = Login.Field()
logout = Logout.Field()
当我直接混入SessionsMutations
对象时,这些突变会出现在顶层并且可以正常工作。
class Query():
pass
class Mutation(SessionsMutations):
pass
schema = graphene.Schema(query=Query, mutation=Mutation)
当我尝试将SessionsMutations定义为字段时,我可以成功向login
提交查询,但是会话始终返回null,并且永远不会运行变异代码。
class Query():
pass
class Mutation():
session = graphene.Field(SessionsMutations)
schema = graphene.Schema(query=Query, mutation=Mutation)
我需要设置什么以便可以接受有组织的突变?