【发布时间】:2020-07-27 07:33:24
【问题描述】:
我想使用 graphQL 查询从 this 条目中提取“唯一蛋白质链:1”行。
我知道这是我要使用的查询:
{
entry(entry_id: "5O6C") {
rcsb_entry_info {
polymer_entity_count_protein
}
}
}
如果我使用graphQL接口here,我可以看到输出:
{
"data": {
"entry": {
"rcsb_entry_info": {
"polymer_entity_count_protein": 1
}
}
}
}
有我想要的信息:“polymer_entity_count_protein”:1
我想通过 python 运行这个查询,以便它可以被输入到其他管道(并且还处理多个 ID)。
我发现 graphene 是一个可以进行 graphQL 查询的库,这是 hello world 示例,我可以在我的机器上使用它:
import graphene
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="world"))
def resolve_hello(self, info, name):
return name
schema = graphene.Schema(query=Query)
result = schema.execute('{ hello }')
print(result.data['hello']) # "Hello World"
我不明白如何将两者结合起来。有人可以告诉我如何使用感兴趣的查询编辑我的 python 代码,所以最后打印的是:
'506C 1'
我已经看到了一些关于石墨烯/graphQL 的其他示例/查询:例如here;除了我不明白如何使我的具体示例工作。
根据下面的答案,我跑了:
进口石墨烯
class Query(graphene.ObjectType):
# ResponseType needs to be the type of your response
# the following line defines the return value of your query (ResponseType)
# and the inputType (graphene.String())
entry = graphene.String(entry_id=graphene.String(default_value=''))
def resolve_entry(self, info, **kwargs):
id = kwargs.get('entry_id')
# as you already have a working query you should enter the logic here
schema = graphene.Schema(query=Query)
# not totally sure if the query needs to look like this, it also depends heavily on your response type
query = '{ entry(entry_id="506C"){rcsb_entry_info}'
result = schema.execute(query)
print("506C" + str(result.data.entry.rcsb_entry_info.polymer_entity_count_protein))
但是,我得到:
Traceback (most recent call last):
File "graphene_query_for_rcsb.py", line 18, in <module>
print("506C" + str(result.data.entry.rcsb_entry_info.polymer_entity_count_protein))
AttributeError: 'NoneType' object has no attribute 'entry'
【问题讨论】:
标签: python graphql graphene-python