我对GraphQL很陌生,想创建一些数据计算API。我已经定义了一些我想在自己的模式中使用的类的方法。这是一个最小的工作示例:
import graphene
class LocationData:
def __init__(self, latitude, longitude, temperature):
self.lat = latitude
self.lng = longitude
self.tmp = temperature
def first_metric(self):
return self.lat + self.lng
def second_metric(self):
return self.lat / self.tmp ** 2
class GeoInput(graphene.InputObjectType):
lat = graphene.Float(required=True)
lng = graphene.Float(required=True)
tmp = graphene.Float(required=True)
class FirstField(graphene.ObjectType):
first_metric = graphene.Float()
class SecondField(graphene.ObjectType):
second_metric = graphene.Float()
third_metric = graphene.Float()
class Query(graphene.ObjectType):
first = graphene.Field(FirstField, geo=GeoInput(required=True))
second = graphene.Field(SecondField, geo=GeoInput(required=True))
def resolve_first(self, info, geo):
data = LocationData(geo.lat, geo.lng, geo.tmp)
return FirstField(first_metric=data.first_metric())
def resolve_second(self, info, geo):
data = LocationData(geo.lat, geo.lng, geo.tmp)
value1 = data.second_metric()
value2 = value1+300
return SecondField(second_metric=value1,
third_metric=value2)
查询当前如下所示:
query{
first(geo: {lat: 30, lng: 20, tmp:2}){
firstMetric
}
second(geo: {lat: 30, lng: 20, tmp:2}){
secondMetric
thirdMetric
}
}
在这里,我想知道如何共享我的LocationData对象,这样它只能被初始化一次,而不是对两个resolve函数都可用的方法?我在某些文档中找不到任何示例。因此,喜欢或类似:
class Query(graphene.ObjectType):
first = graphene.Field(FirstField, geo=GeoInput(required=True))
second = graphene.Field(SecondField, geo=GeoInput(required=True))
data = LocationData(geo.lat, geo.lng, geo.tmp)
def resolve_first(self, info, geo):
return FirstField(first_metric=self.data.first_metric())
def resolve_second(self, info, geo):
value1 = self.data.second_metric()
value2 = value1+300
return SecondField(second_metric=value1,
third_metric=value2)
答案 0 :(得分:0)
最简单的方法是简单地将所有可能的度量标准放在同一ObjectType中,然后为其分配解析器。
class Metrics(graphene.ObjectType):
first_metric = graphene.Float()
second_metric = graphene.Float()
third_metric = graphene.Float()
class Query(graphene.ObjectType):
metrics = graphene.Field(Metrics, geo=GeoInput(required=True))
def resolve_metrics(self, info, geo):
data = LocationData(geo.lat, geo.lng, geo.tmp)
return Metrics(first_metric=data.first_metric(),
second_metric=data.second_metric(),
third_metric=data.second_metric()+300)
执行此操作以避免不必要的计算的另一种方法可能是在函数类和每个字段的解析器中添加__init__
方法。例如:
class Metrics(graphene.ObjectType):
def __init__(self, data):
self.geo_data = data
first_metric = graphene.Float()
second_metric = graphene.Float()
third_metric = graphene.Float()
def resolve_first_metric(self, info):
print 'First'
return self.geo_data.first_metric()
def resolve_second_metric(self, info):
print 'Second'
return self.geo_data.second_metric()
def resolve_third_metric(self, info):
print 'Third'
return self.geo_data.second_metric() + 300
class Query(graphene.ObjectType):
metrics = graphene.Field(Metrics, geo=GeoInput(required=True))
def resolve_metrics(self, info, geo):
data = LocationData(geo.lat, geo.lng, geo.tmp)
return Metrics(data)
如果我们使用以下命令执行此查询:
query {
metrics (geo: {lat: 30, lng: 20, tmp:2}) {
firstMetric
thirdMetric
}
}
我们得到
{
"data": {
"metrics": {
"firstMetric": 50.0,
"thirdMetric": 307.5
}
}
}
从控制台中,我们可以看到未调用second_metric()
。
First
Third