Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 2

Query repository pullRequests via v4 GraphQL API

Solved! Go to Solution.

Hi all, I'm trying to query the pullRequests from a specific repository, but am getting back a null repository object even though I can query for pullRequests when I ask for all repositories. I'm using an installation token which has "pull_request" read-only permissions, but nothing else.

 

My query is something like this:

query {
  viewer {
    repository(name: "jabberwocky") {
      name
      pullRequests(first: 10) {
        pageInfo {
          endCursor
          hasNextPage
        }
        nodes {
          id
          url
          body
        }
      }
    }
  }
}

But I get back:

{
    "data": {
        "viewer": {
            "repository": null
        }
    }
}

I thought it might be a permissions issue, but when I query for pullRequests on all viewer repositories I get back the pullRequest data I was hoping for. That query looks like this:

query {
  viewer {
    repositories(first: 10) {
        nodes {
            name
            pullRequests(first: 10) {
                pageInfo {
                    endCursor
                    hasNextPage
                }
                nodes {
                    id
                    url
                    body
                }
            }
        }
    }
  }
}

And I get back:

{
    "data": {
        "viewer": {
            "repositories": {
                "nodes": [
                    {
                        "name": "foobar",
                        "pullRequests": {
                            "pageInfo": {
                                "endCursor": null,
                                "hasNextPage": false
                            },
                            "nodes": []
                        }
                    },
                    {
                        "name": "jabberwocky",
                        "pullRequests": {
                            "pageInfo": {
                                "endCursor": "Y3Vyc29yOnYyOpHOEWi7QQ==",
                                "hasNextPage": false
                            },
                            "nodes": [
                                {
                                    "id": "ABC",
                                    "url": "https://github.com/abc/jabberwocky/pull/1",
                                    "body": "- [x] copied\r\n- [x] pasted"
                                },
                                {
                                    "id": "XYZ",
                                    "url": "https://github.com/abc/jabberwocky/pull/2",
                                    "body": ""
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

 

Where am I going wrong?

 

Thanks in advance!

1 Reply
Solution
wei
Pilot Lvl 2
Message 2 of 2

Re: Query repository pullRequests via v4 GraphQL API

Your query works but only for repositories under the owner of the access token. 

 

Use the following instead for any public repo:

{
  repository(owner: "abc", name: "jabberwocky") {
    pullRequests(first: 10) {
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes {
        id
        url
        body
      }
    }
  }
}