gpt4 book ai didi

graphql - 从graphql查询中的嵌套结果中过滤掉零总数

转载 作者:行者123 更新时间:2023-12-04 17:13:26 25 4
gpt4 key购买 nike

我有以下查询可以针对 github graphql API 运行

query userRepositories($cursor: String, $q: String!, $githubId: String!) {
search(query: $q, type: REPOSITORY, first: 100, after: $cursor) {
repositoryCount
pageInfo {
endCursor
startCursor
}
nodes {
... on Repository {
id
name
description
isArchived
isPrivate
nameWithOwner
url
defaultBranchRef {
target {
... on Commit {
history(first: 10, author: {id: $githubId}) {
totalCount
}
}
}
}
}
}
}
}
它返回这样的结果:
{
"data": {
"search": {
"repositoryCount": 103,
"pageInfo": {
"endCursor": "Y3Vyc29yOjEwMA==",
"startCursor": "Y3Vyc29yOjE="
},
"nodes": [
{
"id": "MDEwOlJlcG9zaXRvcnk2MTg1OTczOA==",
"name": "microstates",
"nameWithOwner": "thefrontside/microstates",
"url": "https://github.com/thefrontside/microstates",
"defaultBranchRef": {
"target": {
"history": {
"totalCount": 0
}
}
},
{
"id": "MDEwOlJlcG9zaXRvcnkxNTU5MTUyODc=",
"name": "effection",
"nameWithOwner": "thefrontside/effection",
"url": "https://github.com/thefrontside/effection",
"defaultBranchRef": {
"target": {
"history": {
"totalCount": 16
}
}
}
},
我只对具有 defaultBranchRef.target.history.totalCount 的节点数组感兴趣即大于 0。
所以我对 nodes 的元素 0 不感兴趣数组,但我属于元素 1。
我可以在 graphql 中过滤这个还是我必须在查询之外的代码中这样做?

最佳答案

GraphQL 无法过滤数组,因此如果 API 支持基于 totalCount 的过滤器您可以通过此过滤器,否则您必须在代码中进行过滤。在 JS 中,这很容易:

const filtered = {
...data,
search: {
...data.search,
nodes: data.search.nodes.filter(node => node.defaultBranchRef.target.history.totalCount > 0),
}
};

关于graphql - 从graphql查询中的嵌套结果中过滤掉零总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69069915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com