我正在使用 django graphene 构建 graphql 服务器,它使用 RESTfull API 来获取数据,遵循以下模式:
class DeviceType(graphene.ObjectType):
id = graphene.ID()
reference = graphene.String()
serial = graphene.Float()
class InstallationType(graphene.ObjectType):
id = graphene.ID()
company = graphene.Int()
device = graphene.ID()
class AllDataType(graphene.ObjectType):
device = graphene.Field(DeviceType)
installation = graphene.Field(InstallationType)
class Query(graphene.ObjectType):
installation = graphene.Field(
InstallationType,
device_id=graphene.Int(required=True)
)
all = graphene.Field(
AllDataType,
serial=graphene.Float(required=True)
)
def resolve_installation(self, info, device_id):
response = api_call('installations/?device=%s' % device_id)['results'][0]
return json2obj(json.dumps(response))
def resolve_all(self, info, serial):
response = api_call('devices/?serial=%s' % serial)['results'][0]
return json2obj(json.dumps(response))
我需要做的查询是这样的:
query {
all(serial:201002000856){
device{
id
serial
reference
}
installation{
company
device
}
}
}
所以,我的问题是如何与这两种类型建立关系,如 中所述AllDataType
,resolve_installation
需要device id
和resolve_all
需要设备的序列号。
要解决安装,我需要解析器device id
返回的内容。resolve_all
我怎样才能做到这一点?