【问题标题】:Testing subscriptions using graphene.test in Python Graphene在 Python Graphene 中使用 graphene.test 测试订阅
【发布时间】:2021-03-20 05:17:46
【问题描述】:

graphene-python 中测试订阅的惯用方法是什么?看来graphene.test 中的client.execute 选项只适用于Query 测试。

附:文档中有一个订阅执行示例,但它似乎不是测试库的一部分 (https://docs.graphene-python.org/en/latest/execution/subscriptions/)。

【问题讨论】:

    标签: python graphql graphene-python


    【解决方案1】:

    graphene(3)的预发布版本支持这样订阅:

    import asyncio
    from datetime import datetime
    from graphene import ObjectType, String, Schema, Field
    
    class Query(ObjectType):
        hello = String()
    
        def resolve_hello(root, info):
            return 'Hello, world!'
    
    class Subscription(ObjectType):
        time_of_day = Field(String)
    
        async def resolve_time_of_day(root, info):
            while True:
                yield datetime.now().isoformat()
                await asyncio.sleep(1)
    
    schema = Schema(query=Query, subscription=Subscription)
    
    async def main():
        subscription = 'subscription { timeOfDay }'
        result = await schema.execute_async(subscription)
        async for item in result:
            print(item)
    
    asyncio.run(main())
    

    来源https://github.com/graphql-python/graphene/issues/1099.

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2013-07-04
      • 2020-01-30
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多