1

我刚刚开始学习 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": {}
                }
            ]
        }
    }
}
4

1 回答 1

1

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

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

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

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

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

于 2019-09-25T05:04:35.960 回答