Finding code on GitHub using GraphQL (API v4)

I use the GitHub GraphQL API to search for files / code containing a specific word. A simple (far-fetched) search example, which in this case should find the term “beef” in the files located in the “recipes” (repo) for “someuser” (owner for the repo), is shown below:

{ search(query: "beef repo:someuser/recipes", type: REPOSITORY, first: 10) { repositoryCount edges { node { ... on Repository { name } } } } } 

I tried this in the GitHub GraphQL Explorer ( https://developer.github.com/v4/explorer/ ) and got zero search results, which is incorrect as I can confirm that the word ("beef" in the example above) is in files in the repo:

 { "data": { "search": { "repositoryCount": 0, "edges": [] } } } 

When I try to use this with the GitHub REST API (v3) via curl, I definitely get the results:

 curl --header 'Accept: application/vnd.github.v3.raw' https://api.github.com/search/code?q=beef+repo:someuser/recipes 

... Therefore, I know that the request (REST v3 API) is valid, and I understand that the query string in the GraphQL API (v4) is identical to the string for the REST API (v3).

My questions:

  • Am I using the GitHub GraphQL API (v4) incorrectly or setting the query string incorrectly, or am I trying to use functionality that is not yet supported?
  • Is there an example of how to do this so that someone can provide (or a link) that illustrates the code search for specific words?
+15
github github-api graphql
source share
2 answers

Type: CODE is not yet supported. You cannot search for code using graphql right now.

Your understanding is correct. It’s just that you are missing one piece. The search you do is against type: REPOSITORY . if you replace your search with

search(query: "beef", type: REPOSITORY, first: 10) {

you will get all the repositions with beef in their name.

+10
source share

You can add qualifiers if you want to search for "beef" only by name and not modify the query as

search (query: "beef in: name", enter: REPOSITORY, first: 10) {

For more information you can look at https://help.github.com/en/articles/searching-for-repositories#search-based-on-the-contents-of-a-repository

0
source share

All Articles