Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphene mutation not mapping Models in SQL Alchemy

I am trying to perform mutation on User models declared using SQL ALCHEMY. Here is the code for my models.py file

# blog/models.py
from sqlalchemy import *
from sqlalchemy.orm import (scoped_session, sessionmaker, relationship,
                            backref)
from sqlalchemy.ext.declarative import declarative_base    
engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
# We will need this for querying
Base.query = db_session.query_property()

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key= True)
    name = Column(String)
    email = Column(String)
    posts = relationship("Post", backref="user")

class Post(Base):
    __tablename__ = 'post'
    id = Column(Integer, primary_key= True)
    title = Column(String)
    text = Column(Text)
    user_id = Column(Integer, ForeignKey('user.id'))

This is Schema.py file

import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from models import db_session,User as UserModel, Post as PostModel
from sqlalchemy import *

class User(SQLAlchemyObjectType):
    class Meta:
        model = UserModel
        interfaces = (relay.Node, )

class Post(SQLAlchemyObjectType):
    class Meta:
        model = PostModel
        interfaces = (relay.Node, )

class CreateUser(graphene.Mutation):
    class Input:
        name = graphene.String()

    ok = graphene.Boolean()
    user = graphene.Field(User)

    @classmethod
    def mutate(cls, instance, args, context, info):
        new_user = User(name=args.get('name'))

        db_session.add(new_user)
        db_session.commit()
        ok = True
        return CreateUser(user=new_user, ok=ok)

class Query(graphene.ObjectType):
    node = relay.Node.Field()
    user = relay.Node.Field(User)
    allUsers = SQLAlchemyConnectionField(User)

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

schema = graphene.Schema(query=Query, mutation = MyMutations, types = [User, Post])

When i try performing following mutation, this is the error i get :

--Query--
    mutation Test{
      createUser(name:"tess"){
        ok
        user{
          name
        }
      }
    }

    --Result--
    {
      "errors": [
        {
          "message": "Class 'schema2.User' is not mapped",
          "locations": [
            {
              "line": 2,
              "column": 3
            }
          ]
        }
      ],
      "data": {
        "createUser": null
      }
    }
like image 366
user2714410 Avatar asked Mar 31 '17 10:03

user2714410


1 Answers

You are trying to create a user with the wrong class. It seems you meant UserModel when you are calling the line User(name=args.get('name'))

The error is correct in that the SQLAlchemyObjectType User is not mapped, the model User which you imported as UserModel is.

like image 105
Yacine Filali Avatar answered Sep 28 '22 20:09

Yacine Filali