我有这样的 schema.py:
import graphene
from app import db
from graphene import relay
from app.models import User as UserModel, Event as EventModel
from graphene_sqlalchemy import SQLAlchemyConnectionField, SQLAlchemyObjectType
class User(SQLAlchemyObjectType):
class Meta:
model = UserModel
interfaces = (relay.Node, )
class UserConnection(relay.Connection):
class Meta:
node = User
class Event(SQLAlchemyObjectType):
class Meta:
model = EventModel
interfaces = (relay.Node, )
class EventConnections(relay.Connection):
class Meta:
node = Event
class CreateUser(graphene.Mutation):
class Arguments:
username = graphene.String(required=True)
fname = graphene.String(required=True)
surname = graphene.String(required=True)
email = graphene.String(required=True)
password = graphene.String(required=True)
user = graphene.Field(lambda: User)
def mutate(self, info, username, fname, surname, email, password):
user = UserModel.query.filter_by(username=username).first()
if user is None:
user = UserModel(username=username, fname=fname, surname=surname, email=email, password=password)
else:
return None
db.session.add(user)
db.session.commit()
return CreateUser(user=user)
class Query(graphene.ObjectType):
node = relay.Node.Field()
# queries that return individual models
user = graphene.Field(lambda: User, username=graphene.String())
event = graphene.Field(lambda: Event, title=graphene.String())
# queries that return all models of given type
all_users = SQLAlchemyConnectionField(UserConnection)
all_events = SQLAlchemyConnectionField(EventConnections)
# resolvers
def resolve_user(self, info, **kwargs):
query = User.get_query(info)
# id = kwargs.get("id")
username = kwargs.get('username')
return query.filter(UserModel.username == username).first()
def resolve_event(self, info, **kwargs):
query = Event.get_query(info)
# id = kwargs.get("id")
title = kwargs.get('title')
return query.filter(EventModel.title == title).first()
class Mutation(graphene.ObjectType):
create_user = CreateUser.Field()
schema = graphene.Schema(query=Query, mutation=Mutation, types=[User, Event])
我的模型是这样的:
from app import db
from datetime import datetime
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(30), unique=True, nullable=False)
fname = db.Column(db.String(20), nullable=False)
surname = db.Column(db.String(35), nullable=False)
is_verified = db.Column(db.Boolean(), default=False)
profile_pic = db.Column(db.Text(), default="default.jpg")
email = db.Column(db.String(120), nullable=False)
password = db.Column(db.String(50), nullable=False)
events = db.relationship("Event", backref="organizer", lazy=True)
def __repr__(self):
print(f"User({self.id}, {self.name}, {self.surname}, {self.email}")
class Event(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(120), nullable=False)
date_posted = db.Column(db.String(50), nullable=False, default=datetime.now().isoformat())
description = db.Column(db.Text)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
因此,如果我想获取所有用户,请输入以下内容:
query{
allUsers{
edges{
node{
id
username
email
}
}
}
}
当我得到 id 时,我得到类似的东西,"id": "VXNlcjoy"
但在我的模型中,id 是一个 int。如果我确实将我的解析器更改为按前面提到的 id 过滤,sqlalchemy 会抛出一个错误,因为数据库中的 id 是整数。然后我更改我的解析器以接受 id 作为整数,并按真实 id 过滤。在该更改之后使用解析器进行过滤但会发生这种情况:
{
user(id: 1){
id
}
}
回复:
{
"data": {
"user": {
"id": "VXNlcjoy"
}
}
}
我想了一下,我认为有两种方法:
- 让 graphql 将位于数据库中的整数 id 返回给我
- 以某种方式使解析器通过 graphql 给出的字符串 id 查询用户和事件(“VXNlcjoy”)
我怎样才能做到这一点?