2

Have this mutation

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)

Before executing the above mutation in graphiql, I add the request header "Authorization": "JWT <token>" so that the user is authorized. But I get the error graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'fields'. The error doesn't occur when I remove the header. It also works fine when I include it in requests for queries. Am I doing something wrong? I need the authorization to happen for mutations too.

I tracked the Traceback and it leads to file .../site-packages\graphql_jwt\middleware.py. It appears it's a bug in the package in function allow_any() line 18 field = info.schema.get_type(operation_name).fields.get(info.field_name). New to this I need help.

I'm using graphene-django==2.15.0 and django-graphql-jwt==0.3.4

K.Nehe
  • 424
  • 10
  • 22

1 Answers1

2

The allow_any function that comes with django-graphql-jwt is expecting somehow to be used with Queries not Mutations. So you may overwrite the allow_any function by adding the native try/except block:

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

and in your settings.py you have to add the path of the overwritten allow_any function:

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

I hope this may solve your problem as it worked with me

S.B
  • 13,077
  • 10
  • 22
  • 49