Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for code in GitHub using GraphQL (v4 API)

I am using the GitHub's GraphQL API to search for files/code containing a particular word. A simple (contrived) example of a search which in this case is to find the term "beef" in files located in "recipes" (the repo) for "someuser" (the 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 GitHub's GraphQL Explorer (https://developer.github.com/v4/explorer/) and receive zero results from the search which is incorrect as I can confirm that the word ("beef" in the example above) is in the files in the repo:

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

When I try this using GitHub's REST API (v3) via curl, I definitely get results:

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

... So I know that the query (REST v3 API) is valid, and my understanding is that the query string in the GraphQL (v4) API is identical to that for the REST (v3) API.

My questions are:

  1. Am I incorrectly using the GitHub GraphQL (v4) API or am I specifying the query string improperly, or am I trying to use functionality that is not yet supported?
  2. Is there an example of how to do this that someone can provide (or link to) that illustrates how to search code for specific words?
like image 493
Eric Broda Avatar asked Jul 28 '17 20:07

Eric Broda


1 Answers

Type: CODE is not supported yet. There is no way you can search the code using graphql right now.

Your understanding is right. Just that you are missing one piece. The search you are doing is happening against the type: REPOSITORY. if you replace your search with

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

you will get all the repos having beef in their name.

like image 127
sreenivas Avatar answered Sep 28 '22 11:09

sreenivas