Github Create Pull Request with GraphQL

GraphQL API allows for query and mutation (update) operations which can be executed in GitHub’s GraphQL Explorer. Below example creates a PR. First, find the repository id:

{
  repository(owner: "gliptak", name: "githubapitest1") {
    id
  }
}

The repository id returned to be used to create the pull request:

{
  "data": {
    "repository": {
      "id": "R_kgDOGS1otA"
    }
  }
}

The mutation (update) creates a new PR from an existing branch (additional details could have been submitted):

mutation {
  createPullRequest(input: {
    repositoryId: "R_kgDOGS1otA"
    title: "my PR",
    baseRefName: "master",
    headRefName: "patch-1"}) {
    pullRequest {
      url
    }
  }
}

Resulting JSON displays the URL of PR created:

{
  "data": {
    "createPullRequest": {
      "pullRequest": {
        "url": "https://github.com/gliptak/githubapitest1/pull/1"
      }
    }
  }
}

At the time of publishing, @export directive allowing to chain variables between queries is under consideration so variable substitution has to happen manually or programmatically.

comments powered by Disqus

Related