【问题标题】:Modify the context in Ariadne before the resolvers在解析器之前修改 Ariadne 中的上下文
【发布时间】:2020-07-09 00:04:16
【问题描述】:

context 的内容在传递给解析器函数之前如何修改?

【问题讨论】:

    标签: python graphql ariadne-graphql


    【解决方案1】:

    查看特殊 Ariadne 类型 ContextValue 的文档。

    GraphQL 类接受关键字参数context_value。它可以是任何类型,并将被设置为上下文。

    如果传递了一个可调用对象,那么它将以request 作为参数调用。

    所以:

    1. 创建一个函数来构建所需的上下文

      def get_context_value(request):
          return {'request': request, 'test': "TEST"}
      
    2. 在 GraphQL 初始化时传递函数:

      app = GraphQL(
          schema,
          context_value=get_context_value,
          debug=True,
      )
      

    解析器中的上下文值:

    {'request': <starlette.requests.Request object at 0x7fc363dbf370>, 'test': 'TEST'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2013-04-06
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 2019-12-05
      • 2012-07-19
      • 1970-01-01
      相关资源
      最近更新 更多