Django GraphQL订阅未返回任何数据

时间:2020-01-20 19:17:15

标签: django graphql subscription graphene-django

我正在使用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

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

在定义这样的订阅时,我遇到的常见问题之一是在使用id进行过滤时类型不匹配。

由于Author是一个字符串,如果event.instance.AuthorID是Django主键(一个int值),则

event.instance.AuthorID == Author

将返回False

您可以通过将Author的值类型转换为int来解决此问题,如下所示:

event.instance.AuthorID == int(Author)