Graphene/GraphQL 查找特定列值
Graphene/GraphQL find specific column value
出于某种原因,我不知道如何在我的 SQLAlchemy 数据库中简单地找到特定的数据。
在 graphene-python documentation 中,它只是执行此查询以匹配 id(这是一个字符串):
book(id: "Qm9vazow") {
id
title
}
现在这是我的 Flask-Graphene-SQLAlchemy BookSchema 代码,我想找到一个特定的标题而不是 ID:
class BookModel(db.Model):
__table__ = db.Model.metadata.tables['books_book']
# Schema Object
class BookSchema(SQLAlchemyObjectType):
class Meta:
model = BookModel
interfaces = (relay.Node, )
# Connection
class BookConnection(relay.Connection):
class Meta:
node = BookSchema
# GraphQL query
class Query(graphene.ObjectType):
node = relay.Node.Field()
allBooks = SQLAlchemyConnectionField(BookConnection)
schema = graphene.Schema(query=Query, types=[BookSchema])
当我 运行 这个查询时,一切正常并且 returns 3 书名:
{
"query": "{ allBooks(first: 3) { edges { node { title } } } }"
}
但是,一旦我尝试匹配特定标题,它就会停止工作。例如:
# I tried all these queries, none worked
1. { allBooks(title: \"some book title\") { edges { node { title } } } }
2. { allBooks(title: \"some book title\") { title }
3. { allBooks(title: 'some book title') { edges { node { title } } } }
错误:"Unknown argument \"title\" on field \"allBooks\" of type \"Query\"."
我一定是犯了一个我没有看到的小错误,但我想不通。我花了几个小时试图解决这个问题。
问题:如何匹配标题并让return成为object?我做错了什么?
想通了!请参阅下面的更改。
class Query(graphene.ObjectType):
node = relay.Node.Field()
all_books = SQLAlchemyConnectionField(BookConnection)
# Added this
find_book = graphene.Field(BookSchema, title = graphene.String())
def resolve_find_book(self, info, title):
query = BookSchema.get_query(info)
return query.filter(BookModel.title == title).first()
我的 GraphQL 查询如下所示:
{
"query": "{ findBook(title: \"some book title\") { title } }"
}
希望这对以后的人有所帮助!
出于某种原因,我不知道如何在我的 SQLAlchemy 数据库中简单地找到特定的数据。
在 graphene-python documentation 中,它只是执行此查询以匹配 id(这是一个字符串):
book(id: "Qm9vazow") {
id
title
}
现在这是我的 Flask-Graphene-SQLAlchemy BookSchema 代码,我想找到一个特定的标题而不是 ID:
class BookModel(db.Model):
__table__ = db.Model.metadata.tables['books_book']
# Schema Object
class BookSchema(SQLAlchemyObjectType):
class Meta:
model = BookModel
interfaces = (relay.Node, )
# Connection
class BookConnection(relay.Connection):
class Meta:
node = BookSchema
# GraphQL query
class Query(graphene.ObjectType):
node = relay.Node.Field()
allBooks = SQLAlchemyConnectionField(BookConnection)
schema = graphene.Schema(query=Query, types=[BookSchema])
当我 运行 这个查询时,一切正常并且 returns 3 书名:
{
"query": "{ allBooks(first: 3) { edges { node { title } } } }"
}
但是,一旦我尝试匹配特定标题,它就会停止工作。例如:
# I tried all these queries, none worked
1. { allBooks(title: \"some book title\") { edges { node { title } } } }
2. { allBooks(title: \"some book title\") { title }
3. { allBooks(title: 'some book title') { edges { node { title } } } }
错误:"Unknown argument \"title\" on field \"allBooks\" of type \"Query\"."
我一定是犯了一个我没有看到的小错误,但我想不通。我花了几个小时试图解决这个问题。
问题:如何匹配标题并让return成为object?我做错了什么?
想通了!请参阅下面的更改。
class Query(graphene.ObjectType):
node = relay.Node.Field()
all_books = SQLAlchemyConnectionField(BookConnection)
# Added this
find_book = graphene.Field(BookSchema, title = graphene.String())
def resolve_find_book(self, info, title):
query = BookSchema.get_query(info)
return query.filter(BookModel.title == title).first()
我的 GraphQL 查询如下所示:
{
"query": "{ findBook(title: \"some book title\") { title } }"
}
希望这对以后的人有所帮助!