python - 如何登录graphql页面(localhost :8000/graphql) with django

标签 python django graphql

我想使用 @login_required 函数,如下代码所示。 我想检查查询是否正确工作。

import graphene 
from graphql_jwt.decorators import login_required


class Query(graphene.ObjectType):
    user = graphene.Field(UserType)

    @login_required
    def resolve_me(self, info):
        user=info.context.user
        return types.UserProfileResponse(user=user)

我将代码放入graphql(localhost:8000/graphql)

查询{me{user{id}}}}

消息是您没有执行此操作的权限

我想也许我必须给 graphql token 。但我不知道如何给 token 。 你能教我吗?

最佳答案

@login_requireddecorator for a view function (它将 request 作为其第一个参数),您不能在您的方法上使用它。

您必须手动进行检查(if user.is_authenticated ...)或将其移至 View 。后者是更好的选择,因为 View 可以决定在这种情况下如何响应,而您的方法则不能。

关于python - 如何登录graphql页面(localhost :8000/graphql) with django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490806/

相关文章:

graphql - 如何使用模式语言创建泛型?

python: pandas.dataframe.values 没有改变

python - 如何从该类包含的类中导入类

python - ListView 中 M2M 元素的平均值

Django Rest Framework 不显示来自 StreamField 的内容

graphql - 将 GraphQL 类型模块化到单独的文件中

python - 舍入问题

python - 如何允许管理员用户在 Django 中编辑电子邮件模板?

Django Admin Inline FileField 链接在新选项卡中打开

mongodb - apollostack/graphql-server - 如何从解析器获取查询中请求的字段