How to set cookies in Graphene Python mutation?
Wound up setting cookies via middleware.
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
...
In a custom graphql view, views.py
, override the dispatch method to read the cookie and set it.
class MyCustomGraphQLView(GraphQLView):
def dispatch(self, request, *args, **kwargs):
response = super(MyCustomGraphQLView, self).dispatch(request, *args, **kwargs)
# Set response cookies defined in middleware
if response.status_code == 200:
try:
response_cookies = getattr(request, CookieMiddleware.MIDDLEWARE_COOKIES)
except:
pass
else:
for cookie in response_cookies:
response.set_cookie(cookie.get('key'), cookie.get('value'), **cookie.get('kwargs'))
return response