【问题标题】:Django GraphQL subscription not returning any dataDjango GraphQL 订阅不返回任何数据
【发布时间】:2020-05-06 20:02:38
【问题描述】:

我正在使用 Graphene、Django 和 graphene-subscriptions 来定义 GraphQL 订阅。每当创建具有特定Author 的新Book 时,我都会尝试接收更新。我已按照入门指南进行操作,并且正在尝试使用以下代码:

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

我做错了什么?

【问题讨论】:

    标签: django graphql subscription graphene-django


    【解决方案1】:

    我在定义这样的订阅时遇到的一个常见问题是在按id 过滤时出现类型不匹配。

    因为Author是一个字符串,如果event.instance.AuthorID是一个Django主键(一个int值),那么

    event.instance.AuthorID == Author
    

    将返回False

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

    event.instance.AuthorID == int(Author)
    

    【讨论】:

      猜你喜欢
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 2018-12-16
      • 2020-02-15
      • 1970-01-01
      • 2023-01-29
      • 2021-12-05
      相关资源
      最近更新 更多