如何将石墨烯解析方法移动到不同的文件?
How to move graphene resolve methods to different files?
我有以下代码。查询是我的根架构。
如果我只有一个 profile
可以在查询中使用 resolve 方法。但是如果架构太大怎么办?
是否要将 resolve_profile
移动到 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)
不,您不能将 resolve_profile
移动到 Profile
,但还有另一种技术可以处理大型架构。您可以将查询拆分为多个文件,并在 Query
中继承每个文件。在此示例中,我将 Query
分解为 AQuery
、BQuery
和 CQuery
:
class Query(AQuery, BQuery, CQuery, graphene.ObjectType):
pass
然后你可以像这样在不同的文件中定义 AQuery
:
class AQuery(graphene.ObjectType):
profile = graphene.ObjectType(Profile)
def resolve_profile(self):
return ...
并将其他代码放入 BQuery
和 CQuery
.
您也可以使用相同的技术来拆分突变。
我有以下代码。查询是我的根架构。
如果我只有一个 profile
可以在查询中使用 resolve 方法。但是如果架构太大怎么办?
是否要将 resolve_profile
移动到 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)
不,您不能将 resolve_profile
移动到 Profile
,但还有另一种技术可以处理大型架构。您可以将查询拆分为多个文件,并在 Query
中继承每个文件。在此示例中,我将 Query
分解为 AQuery
、BQuery
和 CQuery
:
class Query(AQuery, BQuery, CQuery, graphene.ObjectType):
pass
然后你可以像这样在不同的文件中定义 AQuery
:
class AQuery(graphene.ObjectType):
profile = graphene.ObjectType(Profile)
def resolve_profile(self):
return ...
并将其他代码放入 BQuery
和 CQuery
.
您也可以使用相同的技术来拆分突变。