I'm trying to use GitHub's GraphQL API to find a list of repos matching a query but limited to a specific language. However, I can't find anything in the docs relating to the multi variable language filter the typical online search supports or how something like this is typically done with GraphQL.
{
search(query: "language:java", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
}
I want to pass two params on language and show the result but this query
just use string to search. I need to send a request as multi items like this
language:['go','java','javaScript']
As a workaround, you can use aliases to build dynamic query with many search
query targetting a specific language and fragments to avoid repetition of the SearchResultItemConnection
in the query :
{
go: search(query: "language:go", type: REPOSITORY, first: 10) {
...SearchResult
}
java: search(query: "language:java", type: REPOSITORY, first: 10) {
...SearchResult
}
javascript: search(query: "language:javascript", type: REPOSITORY, first: 10) {
...SearchResult
}
}
fragment SearchResult on SearchResultItemConnection {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
Note that it would only work for OR
query (java or javascript or go for the list of languages) but not AND
The request can be built programmatically such as in this python script :
import requests
token = "YOUR_TOKEN"
languages = ["go","java","javaScript"]
query = """
{
%s
}
fragment SearchResult on SearchResultItemConnection {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
"""
searchFragments = "".join([
"""
%s: search(query: "language:%s", type: REPOSITORY, first: 10) {
...SearchResult
}
""" % (t,t) for t in languages
])
r = requests.post("https://api.github.com/graphql",
headers = {
"Authorization": f"Bearer {token}"
},
json = {
"query": query % searchFragments
}
)
print(r.json()["data"])