Search code examples
githubgraphqlgithub-api

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.


Solution

  • 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"])