Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Last active April 10, 2025 12:26
Show Gist options
  • Save SomajitDey/12dce837edea0d5d52e9737efb11c55f to your computer and use it in GitHub Desktop.
Save SomajitDey/12dce837edea0d5d52e9737efb11c55f to your computer and use it in GitHub Desktop.
GraphQL offers many advantages over REST API. Once learned, it's fast, it's efficient, it's fun

Notes and Learning Resources

Examples: Play with these in GitHub GraphQL Explorer

Others' examples

Get commit SHAs for branch(s)

Let the branches be BRANCH-A and BRANCH-B, of repository REPO, owned by OWNER. To avoid conflict, lets alias BRANCHB as ALIAS. Put your own values in place of the uppercase strings.

query {
  repository(owner:"OWNER", name:"REPO", followRenames:true) {
    ref(qualifiedName:"refs/heads/BRANCH-A"){
      target{
        oid
      }
    }
    ALIAS: ref(qualifiedName:"refs/heads/BRANCH-B"){
      target{
        oid
      }
    }
  }
}

✨ This demonstrated a major advantage of GraphQL over REST API. Multiple queries, such as resolving both BRANCH-A and BRANCH-B, could be done using the same request.

Get file contents

Assuming TAG tags a blob, instead of a commit or tree.

query {
  repository(owner:"OWNER", name:"REPO", followRenames:true) {
    ref(qualifiedName:"refs/tags/TAG"){
      target{
        ...on Blob {
          text
        }
      }
    }
  }
}

Get README.md from branch "main"

query {
  repository(owner:"OWNER", name:"REPO", followRenames:true) {
    ref(qualifiedName:"refs/heads/main"){
      target{
        ...on Commit {
          file(path: "README.md"){
            object {
              ...on Blob {
                text
              }
            }
          }
        }
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment