How can I access github repository contents using graphql (v4 api)? - github

If I use github v3 api to access the directory contents of a public repository using the following query:
curl https://api.github.com/repos/w3c/webappsec/contents/
what is the equivalent in graphql?
I can get for example the description of the repository by sending the following to: https://api.github.com/graphql
query TestQuery{
repository(owner:"w3c" name:"webappsec"){
description
}
}
But how can I get the contents of a repository's directory?

You can use object(expression: "branch_name:") and list the tree entries:
{
repository(owner: "w3c", name: "webappsec") {
object(expression: "master:") {
... on Tree {
entries {
name
type
mode
}
}
}
}
}
Try it in the explorer

Related

Getting commit data on File list Github GraphQL API

I'm new to the GraphQL API for Github. I'm trying to get the commit message and pushedAt date on each file, which you typically see on the root webpage of any github repo showing all the files for the branch (e.g. https://github.com/Alamofire/Alamofire ).
.github Updates for Xcode 13.3 (#3576) 3 days ago
Tests Fix 2020 and 2021 Deprecation Warnings (#3555) last month
My graphql seems to be returning nothing ("commitData": {}) in the Explorer for the commit data. What am I doing wrong?
fragment Files on Tree {
entries {
oid
name
type
path
extension
metaData: object {
... on Blob {
byteSize
isBinary
}
}
commitData: object {
... on Commit {
oid
pushedDate
message
}
}
}
}
query Files($owner: String!, $repo: String!, $branch: String!) {
repository(owner: $owner, name: $repo) {
... on Repository {
object(expression: $branch) {
... on Tree {
...Files
}
}
}
}
}
Query Variables:
{
"repo": "Alamofire",
"branch": "HEAD:",
"owner": "Alamofire"
}
This is expected. The TreeEntry object: GitObject does not expose commit information. Rather, it returns a Blob type (for file objects) or Tree type (for directory paths). How do we know this? The documentation for object ("Entry file object") provides a vague hint, but we can be sure by introspecting the GraphQL __typename meta field to get the object type name:
metaData: object {
__typename
}
For a file entry, this results in:
"metaData": {
"__typename": "Blob"
}
Even though Commit also implements the GitObject interface, the TreeEntry type does not return it. In other words, you can't get there from here. Using this API you need a two step query, it seems.

Get info about several repositories from Github Graphql API in a single call

I am trying to create a query to Github GraphQL API that receive a list of repos as a parameter, and returns the info of those repositories in a single API call, does anyone know how to do that?
Something like this (I know this doesn't work)
query myOrgRepos($repos: [String!]!) {
search(query:"""
repo in $repos
""", type: REPOSITORY, first: 100) {
repo: nodes {
... on Repository{
name
description
updatedAt
}
}
}
It took me some time to understand your comment so I will post the answer in full. The key is to use Github's advanced search “syntax” in the query string, see here for examples.
The following query will find all repositories of user “github” as well as the graphql-js repository of user “graphql”:
query {
search(query: "user:github repo:graphql/graphql-js" type: REPOSITORY first: 10) {
nodes {
... on Repository {
name
description
updatedAt
}
}
}
}
My understanding of your question is that you want to input a list of "known" repos and output information about them. In this case you can form a query of the following type.
{
nodes(ids: ["node_id_1", "node_id_2"]) {
... on Repository {
nameWithOwner
createdAt
}
}
}
Here the ids are the node_ids of the repositories you are interested in. You can obtain these by individual GraphQL calls as follows
{
repository(owner: "owner", name: "name") {
id
}
}

GitHub's GraphQL API: How can I get all of the commits I've contributed to a particular public repo?

I'm trying to query GitHub's GraphQL API (v4). What GraphQL query for this API can be used to get all of the commits I've contributed to a particular public repo?
I figured out the solution. (The solution shown below is for getting relevant commits from master, but can easily be adapted for other qualifiedName values.)
First, perform an initial query to obtain user ID:
query {
viewer {
id
}
}
Then the following query can be used to get all contributed commits to a particular repository:
query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
repository(owner: $repository_owner, name: $repository_name) {
ref(qualifiedName: "master") {
target {
... on Commit {
history(author: {id: $user_id}) {
totalCount
nodes {
id
authoredDate
message
}
}
}
}
}
}
}
The query variables take the following form:
{
"repository_owner": "REPOSITORY_OWNER_STRING",
"repository_name": "REPOSITORY_NAME_STRING",
"user_id": "USER_ID_FROM_STEP_1"
}

How to get Readme.MD from Github Graphql API?

The v3 has a specific API for retrieving the readme.md file. But in the new V4 GraphQL, there is no such field in the Repository Object.
Does anyone know how to retrieve the readme file?
Thanks!
There is not yet a specific entity to get the README.md file, but you can retrieve it as you would normally retrieve any other file:
{
repository(owner: "gitpoint", name: "git-point") {
object(expression: "master:README.md") {
... on Blob {
text
}
}
}
}
It looks like because the GitObject implements Blob, you can use the "... on" syntax to access it's properties, which will contain the contents of the object.
In order to access the object in question, pass in the branch and filename with the extension in the format "branch:filename.ext" and retrieve the Blob from the result, and the text from that.
Multiple objects can be retrieved simultaneously, allowing you to check for alternate casings, such as lowercase "readme.md" names. Simply provide aliases for the objects. Example below.
{
repository(owner: "owner", name: "name") {
upCase: object(expression: "master:README.md") {
... on Blob {
text
}
}
object(expression: "master:readme.md") {
... on Blob {
text
}
}
otherFile: object(expression: "master:index.js") {
... on Blob {
text
}
}
}
This may help explain the "... on" syntax.
https://graphql.github.io/graphql-spec/June2018/#sec-Inline-Fragments

GitHub GraphQL to Read Repository Contents

I'm looking for a way to use the GitHub GraphQL to read repository contents (paths) and then provide a second query to grab the contents of the full path. I started heading down this path for the second query, and it's failing. The former is the more important issue for me right now.
query{
viewer {
login
name
repository(name:"myrepo") {
id
descriptionHTML
object(expression: "branch:readme.md") {
id
}
}
}
}
}
You had an extra } in your query, this is why it was failing.
You also want to replace "branch" with the actual branch name ("master" for example)
Here's a complete example that will also get you the file contents:
{
viewer {
login
name
repository(name: "git-point-playground") {
id
descriptionHTML
object(expression: "master:README.md") {
id
... on Blob {
text
}
}
}
}
}