Search code examples
githubgraphqlgithub-apigithub-graphql

How to get all comments along with issues with github graphql


I'am trying this get github issues by their ids through graphql endpoints

And tried this code

{
  repository(name: "reponame", owner: "ownername") {
    issue1: issue(number: 2) {
      title
      createdAt
    }
    issue2: issue(number: 3) {
      title
      createdAt
    }
    issue3: issue(number: 10) {
      title
      createdAt
    }
  }
}

With this, I am able to get back the title, but I am also trying to get all of the comments of the issue. I tried adding comments to the above code, but it didn't work.

I want to modify the above code only.

Thanks in advance!


Solution

  • With GraphQL, you have to select every scalar field that you want. So far, you have selected the scalar fields of Issue.title and Issue.createdAt. Comments, however, are not "scalar values" -- they're objects -- so to get anything out of them, you have to request deeply into the objects all the way to the scalar values.

    Additionally, Comments are a paginated connection, so you also have to define how many you want back and go deep into the connection to get to the "node", which is the object you actually want:

    query {
      repository(name:"reponame", owner: "ownername") {
        issue(number: 2) {
          title
          createdAt
          # first 10 results
          comments(first: 10) {
            # edges.node is where the actual `Comment` object is
            edges {
              node {
                author {
                  avatarUrl
                }
                body
              }
            }
          }
        }
      }
    }