7

I am trying to query GitHub for information about repositories using their v4 graphql. One of the things I want to query is the breakdown of all the languages used in the repo. Or if possible, the breakdown of the languages across all of a user's repos. I have tried the following snippet, but it returns null, where as primary language returns the primary language

languages: {
  edges: {
    node: {
      name
    }
  }
}

The only thing I can find relating to languages is the primary language. But I would like to show stats for a user and the all languages they use either in a single repo or across off their repos.

2 Answers2

11

You are missing the slicing field, here you can put first: 100 to get the first 100 languages for the repository:

{
  user(login: "torvalds") {
    repositories(first: 100) {
      nodes {
        primaryLanguage {
          name
        }
        languages(first: 100) {
          nodes {
            name
          }
        }
      }
    }
  }
}

If you want to have stats per language (eg if you want to know which is the second, third language etc...) I'm affraid this is not currently possible with the graphql API but using the List Languages API Rest for instance https://api.github.com/repos/torvalds/linux/languages

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
5

I wanted to point our something else that may help.

You can get more details about a language (i.e. primary, secondary etc) by looking at the language size. Comparing the totalSize for the whole repo to the size for each language it has.

The following query (example for pytorch) will get the data you need. Put it into the GH's GQL Explorer to check it out.

{
  repository(name: "pytorch", owner: "pytorch") {
    languages(first: 100) {
      totalSize
      edges {
        size
        node {
          name
          id
        }
      }
    }
  }
}

You will get an output of the form

{
  "data": {
    "repository": {
      "languages": {
        "totalSize": 78666590,
        "edges": [
          {
            "size": 826272,
            "node": {
              "name": "CMake",
              "id": "MDg6TGFuZ3VhZ2U0NDA="
            }
          },
          {
            "size": 29256797,
            "node": {
              "name": "Python",
              "id": "MDg6TGFuZ3VhZ2UxNDU="
            }
          }, ...

To get % for each language just do size / totalSize * 100

David Butler
  • 143
  • 1
  • 10