Search code examples
githubgraphqlgithub-api

Get latest release without prerelease in GraphQL


I'm migrating my connection with GitHub REST API to GraphQL API and I'm confused about getting latest release.

When I use this endpoint bellow to get latest release with REST API it will never return Draft releases or prereleases.

/repos/:owner/:repo/releases/latest

But, when I do the same with GraphQL API I can't filter that, using the query bellow I get the latest release but if it is and prerelease I'll have to query again to find another one.

{
  InovaFarmaApi: repository(owner: "precisaosistemas", name: "inovafarma-api") {
    ...releaseData
  }
}

fragment releaseData on Repository {
  releases (last: 2) {
      nodes {
        isPrerelease
      }
    }
}

Can I filter for only release and not Draft releases or prereleases?


Solution

  • It doesn't look like it.

    The GitHub developer documentation has a complete listing of all GraphQL object types, their fields, and their associated parameters. A Repository in particular documents its releases field; that has the Relay connection parameters and an ordering, but the only supported ReleaseOrderField values are CREATED_AT and NAME.

    That means you need to repeat calls to page through the releases until you find one that meets your criteria.

    query GetRelease($owner: String!, $name: String!, $cursor: String) {
      repository(owner: $owner, name: $name) {
        releases(before: $cursor,
                 last: 1,
                 orderBy: {field: CREATED_AT, order: DESC}) {
          pageInfo { hasPreviousPage, startCursor }
          nodes {
            isPrerelease
            ...OtherReleaseData
          }
        }
      }
    }
    

    If you hit a release where isPrerelease is true (which you don't want), and hasPreviousPage is also true, re-run the query passing the previous startCursor value as the cursor argument.