1

有这个突变

class AddStudentMutation(graphene.Mutation):
    class Arguments:
        input = StudentInputType()
    
    student = graphene.Field(StudentType)
    
    @classmethod
    @staff_member_required
    def mutate(cls, root, info, input):
        try:
            _student = Student.objects.create(**input)
        except IntegrityError:
            raise Exception("Student already created")
        return AddStudentMutation(student=_student)

在执行上述突变之前graphiql,我添加了请求标头"Authorization": "JWT <token>",以便对用户进行授权。但我得到了错误graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'fields'。删除标题时不会发生错误。当我将它包含在查询请求中时,它也可以正常工作。难道我做错了什么?我也需要授权才能发生突变。

我跟踪了 Traceback,它导致了 file .../site-packages\graphql_jwt\middleware.py。看来这是功能allow_any() 行 18中的包中的错误field = info.schema.get_type(operation_name).fields.get(info.field_name)。新手,我需要帮助。

我正在使用graphene-django==2.15.0django-graphql-jwt==0.3.4

4

1 回答 1

0

allow_any附带的功能django-graphql-jwt期望以某种方式与查询而不是突变一起使用。allow_any因此,您可以通过添加本机try/except块来覆盖该函数:

def allow_any(info, **kwargs):
    try:
        operation_name = get_operation_name(info.operation.operation).title()
        operation_type = info.schema.get_type(operation_name)

        if hasattr(operation_type, 'fields'):

            field = operation_type.fields.get(info.field_name)

            if field is None:
                return False

        else:
            return False

        graphene_type = getattr(field.type, "graphene_type", None)

        return graphene_type is not None and issubclass(
            graphene_type, tuple(jwt_settings.JWT_ALLOW_ANY_CLASSES)
        )
    except Exception as e:
        return False

并且settings.py您必须添加覆盖allow_any函数的路径:

GRAPHQL_JWT = {
      'JWT_ALLOW_ANY_HANDLER': 'path.to.middleware.allow_any'
}

我希望这可以解决您的问题,因为它对我有用

于 2022-02-28T14:42:48.670 回答