python - 石墨烯:如何为不同的解析器共享 InputObjectType?
问题描述
当输入变量对所有解析器都等效时,我不想多次定义输入变量。我怎样才能做到这一点?
import graphene
class GeoInput(graphene.InputObjectType):
lat = graphene.Float(required=True)
lng = graphene.Float(required=True)
@property
def latlng(self):
return "({},{})".format(self.lat, self.lng)
class Address(graphene.ObjectType):
latlng = graphene.String()
class Test(graphene.ObjectType):
test_string = graphene.String()
class Query(graphene.ObjectType):
address = graphene.Field(Address, geo=GeoInput(required=True))
test = graphene.Field(Test, geo=GeoInput(required=True))
def resolve_address(self, info, geo):
return Address(latlng=geo.latlng)
def resolve_test(self, info, geo):
return Test(test_string="({},{})".format(geo.lat, geo.lng))
schema = graphene.Schema(query=Query)
query = """
query something{
address(geo: {lat:32.2, lng:12}) {
latlng
}
test(geo: {lat:32.2, lng:12}) {
testString
}
}
"""
if __name__ == "__main__":
result = schema.execute(query)
print(result.data["address"]["latlng"],
result.data["test"]["testString"])
有没有办法为整个查询类做这件事,比如初始化变量?
解决方案
推荐阅读
- python - 遍历 Pandas Dataframe 列并将其元素添加到 Python Collections Counter 对象
- c++ - 使用指针对象的 std::vector。有没有办法在超出范围时从向量中删除一个项目?
- airflow - Airflow 元数据中的作业表是什么?
- css - 如何更改 Angular CDK 拖放的 z-index?
- r - 如何在 ggplot2 折线图中创建图例标题
- r - 调整绘图轴(R CoOccurr 包)
- swift - KVO:无法在 iOS 10.3.4 iPhone 5 上解开密钥路径
- powerbi - 数据仓库环境中的 Power BI
- windows - 谁能解释 LNK 文件漏洞 (CVE-2015-0096) 的工作原理?
- vhdl - VHDL 输出端口上不存在驱动程序