如何在石墨烯Python中设置cookies变异?

2024-05-28 18:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

Graphene Python中,当不能访问HttpResponse对象来设置cookie时,应该如何在schema.py中设置cookie?在

我当前的实现是通过捕捉data.operationName重写GraphQLView的分派方法来设置cookie。这涉及到我需要设置cookies的操作名/突变的硬编码。在

在视图.py公司名称:

class PrivateGraphQLView(GraphQLView):
    data = self.parse_body(request)
    operation_name = data.get('operationName')
    # hard-coding === not pretty.
    if operation_name in ['loginUser', 'createUser']:
        ...
        response.set_cookie(...)
    return response

有没有一种更干净的方法来为特定的石墨烯Python突变设置cookies?在


Tags: 对象方法namepy分派datacookieresponse
1条回答
网友
1楼 · 发布于 2024-05-28 18:25:47

最后通过中间件设置cookies。在

class CookieMiddleware(object):

    def resolve(self, next, root, args, context, info):
        """
        Set cookies based on the name/type of the GraphQL operation
        """

        # set cookie here and pass to dispatch method later to set in response
        ...

在自定义graphql视图views.py中,重写dispatch方法以读取cookie并对其进行设置。在

^{pr2}$

相关问题 更多 >

    热门问题