gpt4 book ai didi

sqlalchemy - 在 graphene-sqlalchemy 查询中按 id 过滤

转载 作者:行者123 更新时间:2023-12-03 19:37:36 25 4
gpt4 key购买 nike

如何设置 graphene-sqlalchemy 以按 id 过滤对象?

我想运行查询:

{
marker(markerId: 1) {
markerId
title
}
}

我希望获得一个 Marker 对象,其中markerId 为1,但我在“Query”类型的字段“marker”上收到错误“Unknown argument“markerId”。”

我有两个文件:

模式.py

import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from model import db_session, Marker as MarkerModel

class Marker(SQLAlchemyObjectType):
class Meta:
model = MarkerModel

class Query(graphene.ObjectType):
marker = graphene.Field(Marker)
markers = graphene.List(Marker)

def resolve_markers(self, args, context, info):
return db_session.query(MarkerModel).all()

def resolve_marker(self, args, context, info):
return db_session.query(MarkerModel).first()

schema = graphene.Schema(query=Query)

模型.py

import sqlalchemy as db
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from instance.config import settings

engine = db.create_engine(settings["SQLALCHEMY_DATABASE_URI"])
sm = sessionmaker(bind=engine)
db_session = scoped_session(sm)

Base = declarative_base()

Base.query = db_session.query_property()


class Marker(Base):
__tablename__ = "marker"
marker_id = db.Column(db.Integer, primary_key=True)
latitude = db.Column(db.Float)
longitude = db.Column(db.Float)
title = db.Column(db.String(100))
blurb = db.Column(db.String(65535))

def __repr__(self):
return "<Marker %d: %s>".format([self.marker_id, self.title])

谢谢你的帮助!

最佳答案

A full working demo can be found under https://github.com/somada141/demo-graphql-sqlalchemy-falcon.



考虑以下 SQLAlchemy ORM 类:
class Author(Base, OrmBaseMixin):
__tablename__ = "authors"

author_id = sqlalchemy.Column(
sqlalchemy.types.Integer(),
primary_key=True,
)

name_first = sqlalchemy.Column(
sqlalchemy.types.Unicode(length=80),
nullable=False,
)

name_last = sqlalchemy.Column(
sqlalchemy.types.Unicode(length=80),
nullable=False,
)

简单地包裹在 SQLAlchemyObjectType 中像这样:
class TypeAuthor(SQLAlchemyObjectType):
class Meta:
model = Author

并通过以下方式暴露:
author = graphene.Field(
TypeAuthor,
author_id=graphene.Argument(type=graphene.Int, required=False),
name_first=graphene.Argument(type=graphene.String, required=False),
name_last=graphene.Argument(type=graphene.String, required=False),
)

@staticmethod
def resolve_author(
args,
info,
author_id: Union[int, None] = None,
name_first: Union[str, None] = None,
name_last: Union[str, None] = None,
):
query = TypeAuthor.get_query(info=info)

if author_id:
query = query.filter(Author.author_id == author_id)

if name_first:
query = query.filter(Author.name_first == name_first)

if name_last:
query = query.filter(Author.name_last == name_last)

author = query.first()

return author

GraphQL 查询,例如:
query GetAuthor{
author(authorId: 1) {
nameFirst
}
}

将产生响应:
{
"data": {
"author": {
"nameFirst": "Robert"
}
}
}

如您所见,您可以通过 graphene.Argument 传递命名参数。上课期间 graphene.Field实例化也必须在解析器方法中命名。然而,两者的结合允许您进行各种过滤。

关于sqlalchemy - 在 graphene-sqlalchemy 查询中按 id 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46244375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com