How to get Readme.MD from Github Graphql API? - github

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

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
}
}

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

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

GraphQL schema from a single object JSON file in Gatsby

So I'd like to query a single JSON file which is not an array from Gatsby's GraphQL but I don't really know how to do it.
As far as I understand gatsby-transformer-json docs - it only supports loading arrays and have them accessible via allFileNameJson schema.
My gatsby-config plugins (only the necessary ones for this question):
{
resolve: 'gatsby-source-filesystem',
options: {
name: 'data',
path: `${__dirname}/src/data`
}
},
'gatsby-transformer-json'
And then let's say in src/data i have a something.json file, like this:
{
"key": "value"
}
Now I'd like to query the data from something.json file, but there is no somethingJson schema i can query (tried with Gatsby's GraphiQL).
Could someone point out what am I doing wrong or how can I solve this problem?
Ok, so it is possible to query single-object files as long as they have a parent (folder).
Let's take these parameters:
gatsby-source-filesystem configured to src/data
test.json file positioned at src/data/test.json with { "key": "value" } content in it
Now as the test.json file actually has a parent (data folder) - you can query the fields from test.json like this:
{
dataJson {
key
}
}
But putting those directly in your root folder is a bad practice, because when you will store another json file, i.e. secondtest.json with { "key2": "value2" } content, and will query it with the same query as above, you will get data only from a single node (not sure if it takes first, or last encountered node),
So, the perfect solution for this case is to have your single-object json files stored in separate folders with just one json per folder.
For example you have some "About Me" data:
create a about folder in your src/data
create an index.json file with i.e. { "name": "John" }
query your data
Like this:
{
aboutJson {
name
}
}
That's it.

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
}
}
}
}
}