【问题标题】:How to move graphene resolve methods to different files?如何将石墨烯解析方法移动到不同的文件?
【发布时间】:2021-02-25 04:32:03
【问题描述】:

我有以下代码。查询是我的根模式。 如果我只有一个profile,则可以在查询中使用 resolve 方法。但是如果架构太大怎么办? 无论如何要在 Profile 对象类型中移动 resolve_profile 吗?

import graphene

class Query(graphene.ObjectType):
  profile = graphene.ObjectType(Profile)

  def resolve_profile(self):
    return ...

class Profile(graphene.ObjectType):
  firstName = graphene.String(graphene.String)
  lastName = graphene.String(graphene.String)

【问题讨论】:

  • 我不熟悉石墨烯,但我认为 Query 在定义架构时也被视为任何其他对象类型,因此每个字段本身都应该有自己的解析器

标签: python graphql graphene-python


【解决方案1】:

不,您不能将resolve_profile 移动到Profile,但是还有另一种技术可以处理大型架构。您可以将查询拆分为多个文件并继承Query 中的每个文件。在本例中,我将Query 分解为AQueryBQueryCQuery

class Query(AQuery, BQuery, CQuery, graphene.ObjectType):
   pass

然后你可以像这样在不同的文件中定义AQuery

class AQuery(graphene.ObjectType):
    profile = graphene.ObjectType(Profile)

    def resolve_profile(self):
         return ...

并将其他代码放入BQueryCQuery

您也可以使用相同的技术来拆分您的突变。

【讨论】:

    猜你喜欢
    • 2019-01-02
    • 2021-03-14
    • 1970-01-01
    • 2020-06-29
    • 2019-12-20
    • 2017-05-11
    • 2022-01-24
    • 2017-11-02
    • 2020-09-03
    相关资源
    最近更新 更多