Saran
Saran

Reputation: 99

get github issues by their ids through graphql endpoints

I am trying to get the list of issues by their ids from Github using graphql, but looks like I am missing something or its not possible.

query ($ids:['517','510']!) {
  repository(owner:"owner", name:"repo") {
    issues(last:20, states:CLOSED) {
      edges {
        node {
          title
          url
          body
          author{
            login
          }
          labels(first:5) {
            edges {
              node {
                name
              }
            }
          }
        }
      }
    }
  }
}

The above query is giving me response as below,

{
  "errors": [
    {
      "message": "Parse error on \"'\" (error) at [1, 14]",
      "locations": [
        {
          "line": 1,
          "column": 14
        }
      ]
    }
  ]
}

Kindly help me identify if its possible or that I am doing something wrong here.

Upvotes: 1

Views: 2370

Answers (2)

Bertrand Martel
Bertrand Martel

Reputation: 45503

You can use aliases in order to build a single request requesting multiple issue object :

{
  repository(name: "material-ui", owner: "mui-org") {
    issue1: issue(number: 2) {
      title
      createdAt
    }
    issue2: issue(number: 3) {
      title
      createdAt
    }
    issue3: issue(number: 10) {
      title
      createdAt
    }
  }
}

Try it in the explorer

which gives :

{
  "data": {
    "repository": {
      "issue1": {
        "title": "Support for ref's on Input component",
        "createdAt": "2014-10-15T15:49:13Z"
      },
      "issue2": {
        "title": "Unable to pass onChange event to Input component",
        "createdAt": "2014-10-15T16:23:28Z"
      },
      "issue3": {
        "title": "Is it possible for me to make this work if I'm using React version 0.12.0?",
        "createdAt": "2014-10-30T14:11:59Z"
      }
    }
  }
}

This request can also be simplified using fragments to prevent repetition:

{
  repository(name: "material-ui", owner: "mui-org") {
    issue1: issue(number: 2) {
      ...IssueFragment
    }
    issue2: issue(number: 3) {
      ...IssueFragment
    }
    issue3: issue(number: 10) {
      ...IssueFragment
    }
  }
}

fragment IssueFragment on Issue {
  title
  createdAt
}

The request can be built programmatically, such as in this example script :

import requests

token = "YOUR_TOKEN"
issueIds = [2,3,10]
repoName = "material-ui"
repoOwner = "mui-org"

query = """
query($name: String!, $owner: String!) {
  repository(name: $name, owner: $owner) {
    %s
  }
}

fragment IssueFragment on Issue {
  title
  createdAt
}
"""

issueFragments = "".join([
    """
    issue%d: issue(number: %d) {
      ...IssueFragment
    }""" % (t,t) for t in issueIds
])
r = requests.post("https://api.github.com/graphql",
    headers = {
        "Authorization": f"Bearer {token}"
    },
    json = {
        "query": query % issueFragments,
        "variables": {
            "name": repoName,
            "owner": repoOwner
        }
    }
)
print(r.json()["data"]["repository"])

Upvotes: 9

mnestorov
mnestorov

Reputation: 4494

I don't think you can fetch for issues and pass in an array of integers for their ids.

But you can search for a single issue by id like so (this works for me)

query ($n: Int!) {
  repository(owner:"owner", name:"repo-name") {
    issue (number: $n) {
      state
      title
      author {
        url
      }
    }
  }
}

where $n is {"n": <your_number>} defined.

If you have an array of ids, then you can just make multiple queries to GitHub.

Sadly, with this approach, you cannot specify what the state of the issue to be. But I think the logic is that once you know the issue Id, you shouldn't care what state it is, since you have that exact id.

Upvotes: 0

Related Questions