Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
132 lines (99 sloc) 3.71 KB
title lastmod publishdate author draft description tags type youtube github
Merge a Pull Request with the GitHub GraphQL API
2020-03-21 12:36:06 -0700
2020-03-21 12:36:06 -0700
Jeff Delaney
How to retrieve and merge Pull Requests programmatically with the GitHub GraphQL API.

I recently gave away a sticker to every person who submitted a pull request to this repo. Problem is... over 600 people submitted PRs and it would take days to review and merge them manually. Luckily, GitHub has an awesome GraphQL API that allows us to handle the process in a matter of minutes with a simple node script.

The following snippet demonstrates how to retrieve and merge Pull Requests programmatically with the GitHub GraphQL API.

Note: You must create a Personal Access Token on GitHub to use the API.

Before getting started in the code, it's often a good idea to explore the API in a client like Insomnia or PostMan.

{{< figure src="/img/snippets/graphql-insomnia.png" caption="Using Insomnia client to explore the Github GraphQL API" >}}

Pull Requests Query

Our first goal is to retrieve information about a batch pull requests using a GraphQL query. The graphql-request library will help simplify the process.

{{< file "terminal" "command line" >}}

npm install graphql-request

Query Pull Requests

First, we set a token in the authorization header to authenticate our requests with Github. We can then point to a username & repo to request information about it, like all the open pull requests.

{{< file "js" "index.js" >}}

const { GraphQLClient } = require('graphql-request');

const token = process.env.GITHUB_GQL_TOKEN;

async function main() {
  const endpoint = '';

  const graphQLClient = new GraphQLClient(endpoint, {
    headers: {
      authorization: `Bearer ${token}`

  const query = /* GraphQL */ `
      repository(owner: "fireship-io", name: "git-sticker") {
        pullRequests(first: 1, states: [OPEN]) {
          nodes {
            author {

  const data = await graphQLClient.request(query);

  console.log(JSON.stringify(data, undefined, 2));



Pull Request Mutation

Now that we have some info about the PRs, let's go ahead and merge one with a GraphQL mutation. The mutation requires Pull Request ID variable, which you should have from the retrieved from previous step.

{{< file "js" "index.js" >}}

async function main() {

 // ...omitted

 const id = 'some-pull-request-id';

 const mutation = /* GraphQL */ `
      mutation mergePullRequest($input: MergePullRequestInput!) {
        mergePullRequest(input: $input) {
          pullRequest {

    const variables = {
      input: {
        pullRequestId: id

    const { mergePullRequest } = await graphQLClient.request(mutation, variables);

    console.log(`merged ${mergePullRequest.pullRequest.url}`);


Merged! Part of code in this snippet was used to merge 600+ pull requests in just a few seconds.

You can’t perform that action at this time.