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.
Related
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
}
}
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
I'm migrating my connection with GitHub REST API to GraphQL API and I'm confused about getting latest release.
When I use this endpoint bellow to get latest release with REST API it will never return Draft releases or prereleases.
/repos/:owner/:repo/releases/latest
But, when I do the same with GraphQL API I can't filter that, using the query bellow I get the latest release but if it is and prerelease I'll have to query again to find another one.
{
InovaFarmaApi: repository(owner: "precisaosistemas", name: "inovafarma-api") {
...releaseData
}
}
fragment releaseData on Repository {
releases (last: 2) {
nodes {
isPrerelease
}
}
}
Can I filter for only release and not Draft releases or prereleases?
It doesn't look like it.
The GitHub developer documentation has a complete listing of all GraphQL object types, their fields, and their associated parameters. A Repository in particular documents its releases field; that has the Relay connection parameters and an ordering, but the only supported ReleaseOrderField values are CREATED_AT and NAME.
That means you need to repeat calls to page through the releases until you find one that meets your criteria.
query GetRelease($owner: String!, $name: String!, $cursor: String) {
repository(owner: $owner, name: $name) {
releases(before: $cursor,
last: 1,
orderBy: {field: CREATED_AT, order: DESC}) {
pageInfo { hasPreviousPage, startCursor }
nodes {
isPrerelease
...OtherReleaseData
}
}
}
}
If you hit a release where isPrerelease is true (which you don't want), and hasPreviousPage is also true, re-run the query passing the previous startCursor value as the cursor argument.
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
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
}
}
}
}
}