【问题标题】:graphQL and graphene: How to extract information from a graphQL query using graphene?graphQL 和石墨烯:如何使用石墨烯从 graphQL 查询中提取信息?
【发布时间】: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


    【解决方案1】:

    您是否编写了问题中已经运行的查询的逻辑?是不是用python/graphene?

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

    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();
    
    

    responseType 定义为:

    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()
    
    

    再一次,这不是 testet 或任何东西,我真的不知道你的反应到底是什么。

    【讨论】:

    • 我能问更多关于“ResponseType”的信息吗?当你说“它是你的回应类型”时,你的意思是它应该是“str”还是什么(或者你能告诉我这个选项在哪里吗?)
    • 我已经修改了我的问题,以向您展示我已经尝试过的额外内容,我已经添加了我使用的代码。
    • 对不起,我的回答有一个愚蠢的错误。您需要在查询字符串的末尾添加一个额外的}。但我仍然不完全确定该查询字符串是否可以正常工作
    • 借助 ResponseType,您可以专门设计您的查询给出的响应。例如,如果您有查询:getPerson,您希望有一个 Person 作为响应。该 Person 需要在一个类型中定义。我将在我的答案中添加一个更详细的示例
    猜你喜欢
    • 2017-05-11
    • 2017-05-13
    • 2018-03-11
    • 2020-09-19
    • 2019-08-26
    • 2018-03-22
    • 2020-09-03
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多