我刚刚开始学习graphql,我创建了一个查询,该查询返回了前10个已关闭问题的列表以及一些属性。令我惊讶的是,我在JSON中获得的响应对象有时为空,有时又为非空。回答是随机的。我也曾与邮递员进行过测试。由于我正在使用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": {}
}
]
}
}
}
答案 0 :(得分:1)
node
字段的类型为SearchResultItem
,即interface。接口是一种抽象类型,代表一种或多种实现该接口的对象类型。换句话说,搜索结果中任何特定node
字段的类型都可以是以下几种类型之一-Issue
,PullRequest
,Repository
等。
查询抽象类型(接口或联合)时,必须使用inline fragments来指定要获取的每种可能类型的字段。因此,对于搜索,我们写:
... on Issue {
# some fields here
}
... on PullRequest {
# some other fields here
}
... on Repository {
# yet some other fields here
}
,依此类推。内联片段仅告诉GraphQL对于给定类型返回哪些字段。 它们不会过滤搜索的实际结果。您不必为每种可能的类型都提供内联片段,但是如果您不提供其中的一种,并且结果会返回该类型,它将作为一个空对象返回(再次,因为您没有告诉GraphQL您想要该特定类型的字段)。
GraphQL不提供任何内置方法来过滤,排序或对返回的数据进行其他处理。必要时,由单个服务来实现该功能。在这种特定情况下,即使type
字段上有一个search
自变量,将自变量的值设置为ISSUE
仍会实际上返回两种不同的类型-Issue
和PullRequest
。因此,如果要避免为某些搜索结果接收到空对象,则PullRequest
仍需要另一个内联片段。