如何将http-请求-会话对象放入石墨烯模式?
我在请求会话中存储了需要通过访问的值。一种可能的解决方案是将会话id发送到前端,然后将其传递到post请求中,但这似乎不是一个好的解决方案。
石墨烯有一个context_value,但我不明白我是如何工作的。
在我的Django视图中,我说:
schema = graphene.Schema()
schema.execute('{ viewer }', context_value={'session': request.session})在我的石墨烯模式中,如果我试图像教程(https://github.com/graphql-python/graphene/blob/master/docs/execution/execute.rst)中所描述的那样做,它会说
'WSGIRequest‘对象没有属性'get’
class Query(graphene.ObjectType):
viewer = graphene.Field(Viewer)
def resolve_viewer(self, info):
info.context.get('session')
print(info.context.session.keys()) #an empty array
return Viewer()发布于 2018-06-05 12:19:30
可以在使用Django会议的解决方法中访问info.context.session。
例如
print("session:", info.context.session)
print("keys:", info.context.session.keys())在解析器中输出给我
session: <django.contrib.sessions.backends.db.SessionStore object at 0x7fa98e6ddac8>
keys: dict_keys(['_auth_user_id', '_auth_user_backend', '_auth_user_hash'])您可以检查一些可以调试的内容:
schema对象,那么您想要的格式是result = schema.execute(query, context_value=request) --有关详细信息,请参阅我在Django中的GraphQL查询不返回任何上的答案https://stackoverflow.com/questions/50697000
复制相似问题