Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 6

graphql search query format

Hi,

I'm looking for all the different properties available in the graphql search query. The query I'm trying to replicate is this one from the V3:

https://api.github.com/search/repositories?q=language:javascript&sort=stars&order=desc

 

Search through all repo with language JavaScript and sort them by stars

 

In graphql tried

 

query topRepos($query: String!) {
  search(first: 3, query: $query, type: REPOSITORY) {
    repositoryCount
    nodes {
      ... on Repository {
        nameWithOwner
        stargazers {
          totalCount
        }
      }
    }
  }
}


// variables
{
 "query": "language:javascript sort:stars order:desc",
}

But it doesn't work. "sorts" gets picked as a search term and it returns repositories with name containing sort.

 

The documentation is very sparse: https://developer.github.com/v4/query/#search

> query: The search string to look for.

 

How can I achieve this?

5 Replies
Highlighted
Community Manager
Message 2 of 6

Re: graphql search query format

Hi @quentin-sommer,

 

As @lee-dohm illustrates in this message, order does not work as a search parameter for sort in this way. However, adding the -desc or -asc suffix to the sort specifier should.

 

Let us know if that doesn't work for you and we'll troubleshoot further.

 

Thanks!

Highlighted
Copilot Lvl 3
Message 3 of 6

Re: graphql search query format

Hi,

 

Thanks for answering. This query returns the repo with the most stars indeed!

 

query topRepos($query: String!) {
  search(first: 3, query: $query, type: REPOSITORY) {
    repositoryCount
    nodes {
      ... on Repository {
        nameWithOwner
        stargazers {
          totalCount
        }
      }
    }
  }
}


// variables
{
  "query": "language:javascript sort:stars",
}

I have two more issue

The results aren't stable, here are some examples of repo that were returned in the format name (stars)

- freeCodeCamp (297 583), bootstrap (131 099), vue (128 884)

- bootstrap (131 099), d3 (82 644), angularjs (59 394)

 

It can be clearly wrong: d3 with 82 644 stars showing up instead of vue with 128 884 stars.

Also React shows up very rarely with 123 171 stars.

Is this a voluntary limitation from GitHub?

 

My last issue is: May I suggest adding the search query documentation to the main Graphql documentation?

 

Thanks again

 

Highlighted
Community Manager
Message 4 of 6

Re: graphql search query format

Have you tried increasing the number of results you ask for to see if the search results become stable at 10 or 25? It could be a limitation of the underlying engine (I'm just guessing here to try to get you an answer faster) that the minimum search window is something larger and because you ask for only three results, you're getting whatever first three come back in that minimum window.

 

As far as the documentation, I'll pass it along. Thanks for the feedback!

Highlighted
Copilot Lvl 3
Message 5 of 6

Re: graphql search query format

I ran 10 consecutive searches of the 75 first elements and it's definitely not stable. I can see patterns (sometimes the results are the same) But the only way I found to have a really stable result is to run multiple searches and combine the results manually.

Highlighted
Ground Controller Lvl 1
Message 6 of 6

Re: graphql search query format

nodes {
      ... on Repository {
        nameWithOwner
        stargazers(orderBy:{field:STARRED_AT,direction:DESC }) {
          totalCount 
        }
      }
    }

even use `orderBy` key, the field has only `STARRED_AT` value, don’t have a similar value with 'starCount'.
so, the result is order by the time of star when they were created,  instead of obrder by stars count.