【问题标题】:Example of Subscription class using Graphene and Python使用 Graphene 和 Python 的订阅类示例
【发布时间】:2017-11-27 15:47:29
【问题描述】:

我试图弄清楚如何使用 graphene-python 在我的架构中正确定义订阅。到目前为止,我已经实现了查询和突变,但是您如何定义 Subscription 类?

以下是我最初的想法:

class Subscription(graphene.Subscription):
  name = graphene.String()
  # rest of attributes...

  def subscribe(self, args, context, info):
    pass

有人可以提供一个小例子吗?任何帮助将不胜感激!谢谢你:)。

布赖恩

【问题讨论】:

  • 我相信我已经弄明白了。一旦我对代码感到满意,我会在这里发布。

标签: python publish-subscribe graphql graphene-python graphql-subscriptions


【解决方案1】:

因此,经过反复试验,下面的代码将适用于订阅。本质上,订阅可以被视为与查询相同。

class Subscription(graphene.ObjectType):
  # Define subscription attributes, i.e. what you want the user to subscribe to.
  # This part will most likely be defined by your schema.
  subscription_attr = graphene.Int()

  def resolve_events_count(self, args, context, info):
    ## define resolver function once UI provides subscription data...
    return 'Value here defined as graphene class'

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 2011-02-14
    • 1970-01-01
    • 2021-07-08
    • 2012-04-02
    • 1970-01-01
    • 2019-01-19
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多