Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django GraphQL JWT: tokenAuth mutation returns "str object has no attribute decode"

Currently I'm running a basic example of django-graphqljwt from the documentation page. https://django-graphql-jwt.domake.io/en/latest/quickstart.html

import graphene
import graphql_jwt


class Mutation(graphene.ObjectType):
    token_auth = graphql_jwt.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.Verify.Field()
    refresh_token = graphql_jwt.Refresh.Field()


schema = graphene.Schema(mutation=Mutation)

However if I run the tokenAuth mutation it throws me the below error in the GraphiQL interface. Note that if I enter incorrect credentials it throws an "Please enter valid credentials" instead of the below.

{
  "errors": [
    {
      "message": "'str' object has no attribute 'decode'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "tokenAuth"
      ]
    }
  ],
  "data": {
    "tokenAuth": null
  }
}
like image 897
Dipanshu Juneja Avatar asked Jan 13 '21 14:01

Dipanshu Juneja


3 Answers

I found this as an open issue on github for the django-graphql-jwt package and was able to resolve it by resorting to the 1.7.0 version of PyJWT. Currently installed was version 2.0

https://github.com/flavors/django-graphql-jwt/issues/241

like image 102
Dipanshu Juneja Avatar answered Sep 19 '22 23:09

Dipanshu Juneja


I'm using the django-graphql-jwt==0.2.1 without this problem. Apparently, the problem is related to the new version of django-graphql-jwt which is 0.3.0 by now. Or, as you mentioned, you could bound the PyJWT to 1.7.0.

The solution is using these bounded packages in your requirements.txt file as follows:

django-graphql-jwt==0.3.0
PyJWT==1.7.0

Or

django-graphql-jwt==0.2.1
like image 34
Benyamin Jafari Avatar answered Sep 21 '22 23:09

Benyamin Jafari


I faced a similar issue, this was apparently because PyJWT==2.3.0 was not compatible with django-graphql-jwt as Dipanshu mentioned in his answer, It worked after downgrading PyJWT==2.3.0 to PyJWT==1.7.0.

Check if the PyJWT package's version in your virtual environment is greater than 2.0.0 using the below pip command

pip show PyJWT

If it is greater than 2.0.0, use the below pip command to downgrade the package in your virtual environment

pip install --upgrade PyJWT==1.7.0
like image 24
Pavan Varyani Avatar answered Sep 21 '22 23:09

Pavan Varyani