我正在使用Graphene,Django和graphene-subscriptions来定义GraphQL订阅。每当创建具有特定Book
的新Author
时,我都试图接收更新。我遵循了入门指南,并且尝试使用以下代码:
class SubscriptionBook(graphene.ObjectType):
NewBooks = graphene.Field(BookType, Author = graphene.String(required=True))
def resolve_NewBooks(root, info, Author):
return root.filter(
lambda event:
event.operation == CREATED and
isinstance(event.instance, Book) and
event.instance.AuthorID == Author
).map(lambda event: event.instance)
但是它似乎不起作用。看来这行失败了:
event.instance.AuthorID == Author
我在做什么错了?
答案 0 :(得分:0)
在定义这样的订阅时,我遇到的常见问题之一是在使用id
进行过滤时类型不匹配。
由于Author
是一个字符串,如果event.instance.AuthorID
是Django主键(一个int
值),则
event.instance.AuthorID == Author
将返回False
。
您可以通过将Author
的值类型转换为int
来解决此问题,如下所示:
event.instance.AuthorID == int(Author)