Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not import 'smvc.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: module 'graphene' has no attribute 'AbstractType'. #1342

Open
msomali opened this issue Jul 17, 2021 · 5 comments

Comments

@msomali
Copy link

msomali commented Jul 17, 2021

I'm trying to setup JWT Authorization with Graphene but I get this error Could not import 'smvc.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: module 'graphene' has no attribute 'AbstractType'. when I try test it on GraphiQL and Insomnia. Is AbstractType no longer included in Graphene?

This error occurs when I do a query for authorizing a user to login with JWT using Insomnia, the headers are in place Content-Type and Authorization (as instructed here on the tutorial https://www.howtographql.com/graphql-python/4-authentication/ or here on git repo https://github.com/howtographql/howtographql/blob/master/content/backend/graphql-python/4-authentication.md) but it won't login the user. I have django admin user and normal user, they bring same error.

File Path: smvc/users/schema.py
Contents:

from django.contrib.auth import get_user_model

import graphene
from graphene_django import DjangoObjectType

class UserType(DjangoObjectType):
    class Meta:
        model = get_user_model()

class Query(graphene.AbstractType):
    me = graphene.Field(UserType)
    users = graphene.List(UserType)

    def resolve_users(self, info):
        return get_user_model().objects.all()

    def resolve_me(self, info):
        user = info.context.user
        if user.is_anonymous:
            raise Exception('Not logged in!')

        return user

class CreateUser(graphene.Mutation):
    user = graphene.Field(UserType)

    class Arguments:
        username = graphene.String(required=True)
        password = graphene.String(required=True)
        email = graphene.String(required=True)

    def mutate(self, info, username, password, email):
        user = get_user_model() (
            username=username,
            email=email,
        )
        user.set_password(password)
        user.save()

        return CreateUser(user=user)

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

If anyone has a way around to resolve or go around this error, I'll appreciate your help.

My Packages:
python: 3.9.5
django: 3.2.5
django-filter: 2.4.0
django-graphql-jwt: 0.3.2
graphene: 3.0b7
graphene-django: 3.0.0b7
graphql-core: 3.1.5
graphql-relay: 3.1.0
mysqlclient: 2.0.3
pyjwt: 2.1.0

Error Snippets:
Browser
image

Insomnia
image

@s-mrb
Copy link

s-mrb commented Jul 22, 2021

@msomali you are using pre relase version of graphene, please switch to v2 to use AbstractType.
As you can see here, there is abstracttype defined in v2 but if you switch to latest version, v3, then there is no abstracttype defined there yet, v3 is in dev phase.

@s-mrb
Copy link

s-mrb commented Jul 22, 2021

@msomali more-ever it is always good to use same version of dependencies as used by tutorials. You can find here the dependencies versions of this tutorial.

@msomali
Copy link
Author

msomali commented Jul 22, 2021

@msomali you are using pre relase version of graphene, please switch to v2 to use AbstractType.

As you can see here, there is abstracttype defined in v2 but if you switch to latest version, v3, then there is no abstracttype defined there yet, v3 is in dev phase.

Hi @s-mrb I tried to downgrade graphene to v2 but I could not restart the server back. I'm thinking of starting a new project and copy some of the work that have already been done on the current broken project.

Thanks.

@msomali
Copy link
Author

msomali commented Jul 24, 2021

@s-mrb so I decided to start a fresh project but still stuck on authentication with JWT as this time I cannot query the logged in user even though I'm following every step of the tutorial including mutating the tokenAuth and use it with Authorization HTTP header (Authorization: JWT ) with Insomnia. Other settings are in place including middleware and authentication_backends.

Error I get:

{
  "errors": [
    {
      "message": "Not logged in!",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "me"
      ]
    }
  ],
  "data": {
    "me": null
  }
}

My Package List:
django 2.1.4
django-environ 0.4.5
django-filter 2.0.0
django-graphql-jwt 0.1.5
graphene 2.1.9
graphene-django 2.2.0
graphql-core 2.3.2
graphql-relay 2.0.1
mysqlclient 2.0.3
openssl 1.1.1k
pip 21.1.3
promise 2.3
pyjwt 1.7.0
python 3.9.5
pytz 2021.1

settings.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
        'NAME': 'smvc_v2',
        'USER': 'xxxxx',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

GRAPHENE = {
    'SCHEMA': 'smvc_v2.schema.schema',
    'MIDDLEWARES': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]

users/schema.py

from django.contrib.auth import get_user_model

import graphene
from graphene_django import DjangoObjectType

class UserType(DjangoObjectType):
    class Meta:
        model = get_user_model()

class Query(graphene.AbstractType):
    me = graphene.Field(UserType)
    users = graphene.List(UserType)

    def resolve_users(self, info):
        return get_user_model().objects.all()

    def resolve_me(self, info):
        user = info.context.user
        if user.is_anonymous:
            raise Exception('Not logged in!')

        return user

class CreateUser(graphene.Mutation):
    user = graphene.Field(UserType)

    class Arguments:
        username = graphene.String(required=True)
        password = graphene.String(required=True)
        email = graphene.String(required=True)

    def mutate(self, info, username, password, email):
        user = get_user_model() (
            username=username,
            email=email,
        )
        user.set_password(password)
        user.save()

        return CreateUser(user=user)

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

smvc_v2/smvc_v2/schema.py

import graphene
import graphql_jwt

import users.schema

class Query(users.schema.Query, graphene.ObjectType):
    pass

class Mutation(users.schema.Mutation, tamisemi.schema.Mutation, jihakiki.schema.Mutation, graphene.ObjectType):
    token_auth = graphql_jwt.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.Verify.Field()
    refresh_token = graphql_jwt.Refresh.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

Please help.

@Shaheer-rossoneri14
Copy link

Shaheer-rossoneri14 commented Oct 11, 2023

This is for anyone using graphene 3 or later and encounter the error while following the tutorial. AttributeError: module 'graphene' has no attribute 'AbstractType'

Update your code with
class Query(graphene.ObjectType, abstract=True):
instead of
class Query(graphene.AbstractType):

AbstractType is deprecated in version 3 and later of graphene.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants