1

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']

1 Answer 1

1

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
      }
    }
  }
}

Try it in the explorer

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 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"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.