graphQL和graphene:如何使用graphene从graphQL查询中提取信息?

2024-05-16 20:53:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用graphQL查询从this条目中提取行“Unique protein chains:1”

我知道这是我想要使用的查询:

{
  entry(entry_id: "5O6C") {
    rcsb_entry_info {
      polymer_entity_count_protein
    }
  }
}

如果我使用graphQL接口here,我可以看到输出:

{
  "data": {
    "entry": {
      "rcsb_entry_info": {
        "polymer_entity_count_protein": 1
      }
    }
  }
}

有我想要的信息:“聚合物\实体\数量\蛋白质”: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'

我看到了一些关于graphene/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'

Tags: theinfoidhellostringcountresultquery
1条回答
网友
1楼 · 发布于 2024-05-16 20:53:04

您是否编写了问题中已在运行的查询的逻辑?它不是在使用python/石墨烯吗

我不确定我是否正确理解了这个问题,但这里有一个大致的想法:

import graphene


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.Field(ResponseType, entry_id=graphene.String()

    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)

以下是响应类型的示例: 如果你有疑问

 # here TeamType is my ResponseType
get_team = graphene.Field(TeamType, id=graphene.Int())

    def resolve_get_team(self, info, **kwargs):
        id = kwargs.get('id')

        if id is not None:
             return Team.objects.get(pk=id)

        else:
            raise Exception();

响应类型定义为:

class TeamType(DjangoObjectType):
    class Meta:
        model = Team

但您也可以定义不基于模型的响应类型:

class DeleteResponse(graphene.ObjectType):
    numberOfDeletedObject = graphene.Int(required=True)
    numberOfDeletedTeams = graphene.Int(required=False)

你的回答类型应该是这样的:

class myResponse(graphene.ObjectType):
    rcsb_entry_info = graphne.Field(Polymer)


class Polymer(graphene.ObjectType):
    polymer_entity_count_protein = graphene.Int()

同样,这不是测试或任何事情,我真的不知道你的反应到底是什么

相关问题 更多 >