GitHub API v4 Graphql: Get current authorized user organizations and their repositories

I just ran into this very issue today. Unfortunately, as of the timestamp indicated at the bottom of this answer, GitHub’s GraphQL API is not on par with its REST API. The following query would only yield the viewer’s public orgs i.e. what an unauthenticated session would show listed on their github.com profile under “Organizations”.

query getRepositories {
  viewer {
    organizations(first: 100) {
      totalCount
      nodes {
        name
      }
    }
  }
}

And there is no equivalent of octokit.orgs.listForAuthenticatedUser() in their GraphQL schema which basically fetches the REST endpoint, /user/orgs, to list organizations for the authenticated user. From the docs:

/user/orgs only lists organizations that your authorization allows you
to operate on in some way (e.g., you can list teams with read:org
scope, you can publicize your organization membership with user scope,
etc.). Therefore, this API requires at least user or read:org scope.
OAuth requests with insufficient scope receive a 403 Forbidden
response.

In other words, with a personal access token with sufficient scopes, /user/orgs returns the same list shown on Your Organizations page. If you’re authenticated using an OAuth access token, then the list is pretty much the same as shown under “Organization Access” on the user’s Authorized OAuth Apps page for your OAuth app.

Read more here: Source link