如何解决依赖于另一个字段的石墨烯Django字段

时间:2018-08-14 03:58:18

标签: python django graphql graphene-python

是否可以使用一个字段的输出来解析两个字段以生成另一个字段的输出?例如,我需要使用访问令牌生成刷新令牌,但是我不确定如何在刷新令牌解析器中获取访问令牌。

class Tokens(graphene.ObjectType):
    accessToken = graphene.String()
    refreshToken = graphene.String()

    def resolve_accessToken(self, info, **kwargs):
        return "..."

    def resolve_refreshToken(self, info, **kwargs):
        return "..."

1 个答案:

答案 0 :(得分:0)

尝试添加一种方法来生成从刷新方法中调用的访问令牌

class Tokens(graphene.ObjectType):
    access_token = graphene.String()
    refresh_token = graphene.String()

    def get_access_token(self, info, **kwargs):
        if not hasattr(self, '_access_token'):
            # Do stuff to create access token, then stash to only create once
            self._access_token = ...
        return self._access_token

    def resolve_access_token(self, info, **kwargs):
        return self.get_access_token(info, **kwargs)

    def resolve_refresh_token(self, info, **kwargs):
        access_token = self.get_access_token(info, **kwargs)
        # Do stuff to create and return refresh token ...

此外,您应该使用蛇形来调用您的方法和字段,即access_tokenaccessToken