如何从 GraphQL 服务器获取平面数组作为响应

Posted

技术标签:

【中文标题】如何从 GraphQL 服务器获取平面数组作为响应【英文标题】:How to get a flat array as a response from GraphQL server 【发布时间】:2021-03-09 09:59:02 【问题描述】:

我在对 Web 服务进行一些 GraphQL 查询时遇到困难。这个问题有点复杂,希望我能尝试并合理地解释它。

问题源于我感兴趣的字段之一不是标量而是数组。我想根据字符串匹配在该数组中返回一个值,如果我理解正确,仅使用 GraphQL 是不可能的。

所以我想我稍后会在 python 中进行过滤,然后将它们全部抓取。但是在这种情况下,问题是我无法轻松处理这些列,因为它是 pandas 中的嵌入式 json 数组。

以下代码显示了一个最小示例,其中我在 xrefs 的字段中获取嵌套的 json 字符串(名称:值对的数组)而不是数组。

query = \
    f"""
    
      targets(
        filter: 
          facets: [
            facet: "Target Development Level",
            values: ["Tchem"]
          ]
        
      ) 
        targets(top:10)
            xrefs (source: "Ensembl")name
            sym
            tdl
            uniprot

          
      
    
    """
url = 'https://pharos-api.ncats.io/graphql'
r = requests.post(url, json='query': query)
json_data = json.loads(r.text)
df = pd.read_json(json.dumps(json_data['data']['targets']['targets']), orient='records')

我想按优先顺序实现的目标:

    能够从 xrefs 中选择单个值,基于仅使用 GraphQL 对 name 的字符串匹配

    再次从 GQL 获取 xrefs 的字符串数组,而不是名称:值对

    让 pandas/python 解析和展平 xrefs 的 json,以便我得到一个字符串列表,而不是类似:

    ['name': 'ENST00000260967', 'name': 'ENSP00000260967', 'name': 'ENSG00000138395', 'name': 'ENST00000410091', 'name': 'ENSP00000386901', 'name': 'ENST00000434439', 'name': 'ENSP00000412775', 'name': 'ENST00000450471', 'name': 'ENSP00000406472']

【问题讨论】:

【参考方案1】:

类似这样的:

import json
import requests

query = f"""

    targets(
    filter: 
        facets: [
        facet: "Target Development Level",
        values: ["Tchem"]
        ]
    
    ) 
    targets(top:10)
        xrefs (source: "Ensembl")name
        sym
        tdl
        uniprot
        
    

"""

url = "https://pharos-api.ncats.io/graphql"
r = requests.post(url, json="query": query)
data = r.json()["data"]["targets"]["targets"]

# Flatten the xrefs
for row in data:
    row["xrefs"] = [element["name"] for element in row["xrefs"]]

print(json.dumps(data, indent=2))

输出:

[
  
    "xrefs": [
      "ENST00000312988",
      "ENSP00000318197",
      "ENSG00000152086"
    ],
    "sym": "TUBA3E",
    "tdl": "Tchem",
    "uniprot": "Q6PEY2"
  ,
  
    "xrefs": [
      "ENST00000399635",
      "ENSP00000382544",
      "ENSG00000206203"
    ],
    "sym": "TSSK2",
    "tdl": "Tchem",
    "uniprot": "Q96PF2"
  ,
  
    "xrefs": [
      "ENST00000612221",
      "ENSP00000483467",
      "ENSG00000231274"
    ],
    "sym": "SBK3",
    "tdl": "Tchem",
    "uniprot": "P0C264"
  ,
  
    "xrefs": [
      "ENST00000251472",
      "ENSP00000251472",
      "ENSG00000105613"
    ],
    "sym": "MAST1",
    "tdl": "Tchem",
    "uniprot": "Q9Y2H9"
  ,
  
    "xrefs": [
      "ENST00000260967",
      "ENSP00000260967",
      "ENSG00000138395",
      "ENST00000410091",
      "ENSP00000386901",
      "ENST00000434439",
      "ENSP00000412775",
      "ENST00000450471",
      "ENSP00000406472"
    ],
    "sym": "CDK15",
    "tdl": "Tchem",
    "uniprot": "Q96Q40"
  ,
  
    "xrefs": [
      "ENST00000238789",
      "ENSP00000238789",
      "ENSG00000119778"
    ],
    "sym": "ATAD2B",
    "tdl": "Tchem",
    "uniprot": "Q9ULI0"
  ,
  
    "xrefs": [
      "ENST00000358371",
      "ENSP00000351140",
      "ENSG00000176601",
      "ENST00000375844",
      "ENSP00000365004",
      "ENST00000375845",
      "ENSP00000365005",
      "ENST00000392915",
      "ENSP00000376647",
      "ENST00000392917",
      "ENSP00000376649",
      "ENST00000392918",
      "ENSP00000376650"
    ],
    "sym": "MAP3K19",
    "tdl": "Tchem",
    "uniprot": "Q56UN5"
  ,
  
    "xrefs": [
      "ENST00000456354",
      "ENSP00000390423",
      "ENSG00000188782",
      "ENST00000518899",
      "ENSP00000429464"
    ],
    "sym": "CATSPER4",
    "tdl": "Tchem",
    "uniprot": "Q7RTX7"
  ,
  
    "xrefs": [
      "ENST00000370435",
      "ENSP00000359464",
      "ENSG00000119900"
    ],
    "sym": "OGFRL1",
    "tdl": "Tchem",
    "uniprot": "Q5TC84"
  ,
  
    "xrefs": [
      "ENST00000321751",
      "ENSP00000319778",
      "ENSG00000175756",
      "ENST00000338338",
      "ENSP00000340656",
      "ENST00000338370",
      "ENSP00000342676",
      "ENST00000378853",
      "ENSP00000368130"
    ],
    "sym": "AURKAIP1",
    "tdl": "Tchem",
    "uniprot": "Q9NWT8"
  
]

【讨论】:

以上是关于如何从 GraphQL 服务器获取平面数组作为响应的主要内容,如果未能解决你的问题,请参考以下文章

如何从 iOS 中的 hasura graphql 查询中获取错误响应

如何从 REST API 定义多个或嵌套 json 数据的数组类型

如何从我得到的数组中获取特定的数组元素作为调用 axios.get() 调用的函数的响应 [重复]

Webonyx/Graphql-php 入门:如何在 cURL 中获取 API 的响应而不会从 API 实现中获得回声?

如何让 RelayJS 理解来自 GraphQL 的响应是一个项目数组,而不仅仅是一个项目

如何使用引导网格映射图像数组?