基于字段的特定值的GraphQl查询

时间:2019-05-04 00:21:26

标签: graphql github-api github-api-v4

我希望能够使用他们的GraphQl API从GitHub检索特定回购的最新版本。为此,我需要获取isDraft和isPrerelease为false的最新版本。我已经设法获得了第一部分,但无法弄清楚如何执行查询的“哪里”部分。

这是我得到的基本查询(https://developer.github.com/v4/explorer/):

{
  repository(owner: "paolosalvatori", name: "ServiceBusExplorer") {
    releases(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) {
      nodes {
        name
        tagName
        resourcePath
        isDraft
        isPrerelease
      }
    }
  }
}

哪个返回:

{
  "data": {
    "repository": {
      "releases": {
        "nodes": [
          {
            "name": "3.0.4",
            "tagName": "3.0.4",
            "resourcePath": "/paolosalvatori/ServiceBusExplorer/releases/tag/3.0.4",
            "isDraft": false,
            "isPrerelease": false
          }
        ]
      }
    }
  }
}

我似乎找不到办法。部分原因是我是GraphQl的新手(第一次尝试执行查询),并且不确定如何构造问题。

只能基于支持参数的类型(例如下面的存储库和发行版)进行“查询”吗?似乎应该有一种方法可以在字段值上指定过滤器。

存储库:https://developer.github.com/v4/object/repository/

发布:https://developer.github.com/v4/object/releaseconnection/

节点:https://developer.github.com/v4/object/release/

2 个答案:

答案 0 :(得分:1)

  

只能基于支持参数的类型进行一次“查询”

是:GraphQL不会以与SQL相同的方式定义通用查询语言。您不能以服务器和应用程序模式所没有提供的方式对字段结果进行排序或过滤。

  

我希望能够使用他们的GraphQl API从GitHub检索特定回购的最新[非草稿,非预发行]版本。

您已经发现,the releases field on the Repository type没有选择对这些字段进行排序或过滤。相反,您可以一次通过多个GraphQL调用遍历发行版。这些看起来像

query NextRelease($owner: String!, $name: String!, $after: String) {
  repository(owner: $owner, name: $name) {
    releases(first: 1,
             orderBy: {field: CREATED_AT, direction: DESC},
             after: $after) {
      pageInfo { lastCursor }
      nodes { ... ReleaseData } # from the question
    }
  }
}

以与现在运行它相同的方式运行它(我已将标识存储库的信息拆分为单独的GraphQL变量)。您可以将after变量保留为第一个调用。如果(如您的示例所示)返回"isDraft": false, "isPrerelease": false,则表示已设置。如果不是,则需要重试:从响应中的lastCursor中获取值,并运行相同的查询,并将该游标值作为after变量值传递。

答案 1 :(得分:0)

  {
  repository(owner: "paolosalvatori", name: "ServiceBusExplorer") {
    releases(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) {
      nodes(isDraft :false , isPrerelease :false ) {
        name
        tagName
        resourcePath
        isDraft
        isPrerelease
      }
    }
  }
}

或者 请查看GrqaphQL指令,因为有时需要根据那里的值跳过或包含字段以作为响应 @skip或@include

skip指令用于字段或片段时,使我们可以根据某些条件排除字段。

include指令允许我们根据某些条件包含字段

GraphQL Directives