从 graphql 响应返回空 "node" 个对象

Empty "node" objects being returned from graphql response

我刚开始学习 graphql,我创建了一个查询 returns 前 10 个已关闭问题的列表以及一些属性。令我惊讶的是,我得到的 JSON 中的响应对象有时是空的,有时它们是非空的。响应是 random.I 也用邮递员测试过。由于我使用 jackson 将 json 响应映射到 Java 类 并执行一些操作,因此在处理空对象时会抛出异常。

1)基本上,我想要一个关闭问题的非空对象。查询有什么问题吗?如果是,有人可以告诉正确的查询吗?

2)另外,我想知道返回空节点对象背后的逻辑

使用的查询


{
  search(first: 20, type: ISSUE, query: "created:<2019-09-21 state:closed") {
    issueCount
    edges {
      node {
        ... on Issue {
          createdAt
          closedAt
          title
          url
          repository {
            name
          }
        }
      }
    }
  }
}

回复 1

{
    "data": {
        "search": {
            "issueCount": 92339271,
            "edges": [
                {
                    "node": {
                        "createdAt": "2019-09-20T23:59:57Z",
                        "closedAt": "2019-09-21T19:59:32Z",
                        "title": "MJPEG stream won't open",
                        "url": "https://github.com/mpv-player/mpv/issues/6964",
                        "repository": {
                            "name": "mpv"
                        }
                    }
                },
                {
                    "node": {
                        "createdAt": "2019-09-20T23:59:50Z",
                        "closedAt": "2019-09-21T01:19:39Z",
                        "title": "Upgrade from v0.5.0 to v0.6.0 with attached volume failed",
                        "url": "https://github.com/longhorn/longhorn/issues/745",
                        "repository": {
                            "name": "longhorn"
                        }
                    }
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {
                        "createdAt": "2019-09-20T23:58:52Z",
                        "closedAt": "2019-09-21T01:55:15Z",
                        "title": "bad linkage",
                        "url": "https://github.com/signalapp/Signal-Desktop/issues/3608",
                        "repository": {
                            "name": "Signal-Desktop"
                        }
                    }
                },
                {
                    "node": {}
                },
                {
                    "node": {
                        "createdAt": "2019-09-20T23:58:36Z",
                        "closedAt": "2019-09-21T00:57:54Z",
                        "title": "Breaks Some Links on Firefox for Mac",
                        "url": "https://github.com/duckduckgo/duckduckgo-privacy-extension/issues/416",
                        "repository": {
                            "name": "duckduckgo-privacy-extension"
                        }
                    }
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {
                        "createdAt": "2019-09-20T23:56:11Z",
                        "closedAt": "2019-09-23T18:43:30Z",
                        "title": "ci: upload coverage reports from GitHub Actions",
                        "url": "https://github.com/hyperledger/aries-framework-go/issues/314",
                        "repository": {
                            "name": "aries-framework-go"
                        }
                    }
                },
                {
                    "node": {}
                },
                {
                    "node": {
                        "createdAt": "2019-09-20T23:56:07Z",
                        "closedAt": "2019-09-21T02:53:35Z",
                        "title": "0xxx.ws",
                        "url": "https://github.com/NanoMeow/QuickReports/issues/1885",
                        "repository": {
                            "name": "QuickReports"
                        }
                    }
                }
            ]
        }
    }
}

回复 2

{
    "data": {
        "search": {
            "issueCount": 92339271,
            "edges": [
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {
                        "createdAt": "2019-09-20T23:58:36Z",
                        "closedAt": "2019-09-21T00:57:54Z",
                        "title": "Breaks Some Links on Firefox for Mac",
                        "url": "https://github.com/duckduckgo/duckduckgo-privacy-extension/issues/416",
                        "repository": {
                            "name": "duckduckgo-privacy-extension"
                        }
                    }
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                },
                {
                    "node": {}
                }
            ]
        }
    }
}

node 字段的类型是 SearchResultItem,这是一个 interface。接口是一种抽象类型,表示实现该接口的一个或多个对象类型。换句话说,搜索结果中任何特定 node 字段的类型可能是以下几种类型之一 - IssuePullRequestRepository 等 [=25] =]

当查询抽象类型(接口或联合)时,我们必须使用 inline fragments 来指定我们想要获取的每种可能类型的字段。所以对于搜索,我们写:

... on Issue {
  # some fields here
}
... on PullRequest {
  # some other fields here
}
... on Repository {
  # yet some other fields here
}

等等。内联片段仅告诉 GraphQL 给定类型的哪些字段 return。 它们不会过滤搜索的实际结果。您不必为每种可能的类型都提供内联片段,但如果您不提供并且该类型是 returned 在结果中,它将被 returned 作为一个空对象(同样,因为你没有告诉 GraphQL 你想要那个特定类型的字段)。

GraphQL 不提供任何内置方法来过滤、排序或对 returned 数据进行其他操作。如果需要,由个别服务来实现该功能。在这种特殊情况下,即使 search 字段上有一个 type 参数,将参数的值设置为 ISSUE 实际上仍然 return 两种不同的类型—— IssuePullRequest。因此,如果您想避免某些搜索结果收到空对象,您仍然需要 PullRequest 的另一个内联片段。