How do I find the teams I'm a member of on GitHub?
Tags: github, graphql, githubapi, teams
Published: 2021-06-23 18:09:12

## Using the UI

- Navigate to your organization on GitHub
- Navigate to your organization's `Teams`
- In the team-scoped search input, type in `members:me visibility:visible` and run the search
  - Example URL: `https://github.com/orgs/ORG_NAME/teams?query=members%3Ame+visibility%3Avisible` (subject to change at any time)
- The results show the teams that you're a member of, where each team is visible to the wider organization. It's possible to change the visibility to `Secret` or `All` if you prefer.

## Using the API

It's possible to use the [GitHub GraphQL API](https://docs.github.com/en/graphql) to find the teams that you're a member of on GitHub. Here's the query to save you from more scrolling:

```graphql
{
  organization(login: "github") {
    totalCount
    teams(first: 10, role:MEMBER) {
      edges {
        node {
          name
        }
      }
    }
  }
}
```

### But, how did you arrive at that conclusion Francis? 🤔

If you're curious, let's build the query together using the [GitHub GraphQL API Explorer](https://docs.github.com/en/graphql/overview/explorer)!

Once you've signed in, a "starter" query is shown that will fetch your GitHub username:

```graphql
query {
  viewer {
    login
  }
}
```

We can erase all of that that and target an [`Organization`](https://docs.github.com/en/graphql/reference/objects#organization) based on its login and return its name:

```graphql
query {

  # new! replacing the current viewer with organization
  organization(login:"github") {
    name
  }
}
```

Omitting `name` will result in an error since the intent of the executed query is to retrieve one or more available fields:

```json
{
  "errors": [
    {
      "message": "Parse error on \"}\" (RCURLY) at [5, 3]",
      "locations": [
        {
          "line": 5,
          "column": 3
        }
      ]
    }
  ]
}
```

We can build on this query by targeting the organization's teams:

```graphql
{
  organization(login: "github") {

    # new! inclusion of teams and their names
    teams(first: 10) {
      edges {
        node {
          name
        }
      }
    }
  }
}
```

The inclusion of `edges` and `node` are a way of traversing the relationship between a set of objects in GraphQL (check out [GraphQL.org's Learn Page on Pagination](https://graphql.org/learn/pagination/) for more information). Here, we're targeting the `github` organization's teams and limiting it to the first `10` (though, you're welcome to choose any number upto 100, which is the global limit of returned objects).

But, that doesn't answer the question of which teams you're actually on. It only fetches the first 10 teams on the organization.

The documentation indicates that there's an available parameter called `role`. By default, it's a `null` value and totally optional. For our cases, we can either set `role` to `ADMIN` or `MEMBER`. I'm interested in finding teams that I'm solely a member of:

```graphql
{
  organization(login: "github") {

    # new! inclusion of `role:MEMBER`
    teams(first: 10, role:MEMBER) {
      edges {
        node {
          name
        }
      }
    }
  }
}
```

I'm a member of a number of teams, but I don't know exactly _how many_ teams I'm actually a part of. What's great about GitHub's GraphQL implementation is the inclusion of a `totalCount` field that's available on all collections. So, for something like say, `teams`, we can specify `totalCount` as a sibling field to `edges`:

```graphql
{
  organization(login: "github") {

    # new! inclusion of the total number of teams I'm a part of 
    totalCount

    teams(first: 10, role:MEMBER) {
      edges {
        node {
          name
        }
      }
    }
  }
}
```

Once you have the value of `totalCount`, you can replace the number of teams to fetch by that number. If it's more than `100`, you can use cursors to get the next set of teams (check out [this example](https://docs.github.com/en/graphql/guides/using-the-graphql-api-for-discussions#objects) to get an idea of how this works).