GraphQL API: How to fetch all repositories that the current user has access to

I’m trying to find a GraphQL equivalent of the /user/repos resource: https://developer.github.com/v3/repos/#list-your-repositories

> List repositories that the authenticated user has explicit permission (:read, :write, or :admin) to access.

I’ve tried following query but that only returns repositories under my account, and not the other repositories I have access to (organizational repos - both public and private):

{
  viewer {
    repositories(first: 100, after: properCursorHereOrOmitIfFirstPage, orderBy: {field:NAME, direction:ASC}) {
      pageInfo {hasNextPage, endCursor}
      nodes {
        name
        url
        isPrivate
        owner {
          login
        }
        defaultBranchRef {
          name
        }
      }
    }
  }
}

EDIT: 

I’ve also tried to use affiliations but without success  - still returns only repositories directly under my “jumarko” account:

{
  viewer {
    repositories(first: 100, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER]) {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes{
        name
          owner {
            login
          }
        }
      }
   }
 }
2 Likes

try repositoriesContributedTo instead of repositories

Thanks for the suggestion but that doesn’t sound right.

I want to get a list of all repositories I have access to, not only the ones I’ve contributed too.

As mentioned before, I want an equivalent of _user/_repos endpoint of v3 API, which for more returns 2300+ repositories while the “equivalent” GraphQL API query (see my previous post) returns only 207 repositories.

repositoriesContributedTo I contributed to returns only 85 repos (with includeUserRepositories set to true).

Hey @jumarko,

Thanks for being here, have you tried? 

{
  viewer {
    repositories(first: 100) {
      totalCount
      nodes {
        nameWithOwner
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}
1 Like

@andreagriffiths11 thanks for the suggestion but that does the same thing as my first query.

Seems to be already reported and awaiting for GitHub feedback: https://github.community/t5/GitHub-API-Development-and/Grapql-viewer-repositories-gets-only-repositories-where-the-user/m-p/14318?collapse_discussion=true&page=2&q=graphql%20user%20repositories&search_type=thread

@jumarko Did you find a way to get all the repos. I really don’t wanna use v3 for this.

@krasimir No, I use V3 rest api to load list of repos.

Ok, fair enough. I’ll do the same for the time being.

I found the solution!

Add the following parameters to your repositories query: affiliations:[OWNER, ORGANIZATION_MEMBER, COLLABORATOR], ownerAffiliations:[OWNER, ORGANIZATION_MEMBER, COLLABORATOR]

Not sure why this is necessary to retrieve private organisation repos and not public organisation repos, but nevertheless it works.

6 Likes