Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 623
Saran Avatar asked Mar 09 '26 06:03

Saran


1 Answers

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 python 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"])
like image 89
Bertrand Martel Avatar answered Mar 11 '26 09:03

Bertrand Martel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!