Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve all users from a GitLab deployment using the GraphQL interface?

I am trying to load user information from GitLab so that I can associate usernames with issues. When querying for issues, the assignee username is not directly available. Instead, a user ID is available.

If I execute this GraphQL query using the /-/graphql-explorer endpoint on my GitLab deployment:

query {                                                                                                                                                                                       
  users {                                                                                                                                                                                     
    nodes {                                                                                                                                                                                   
      id                                                                                                                                                                                      
      name                                                                                                                                                                                    
      username                                                                                                                                                                                
    }                                                                                                                                                                                         
  }
}

then 91 users are returned. This is clearly not all users on the deployment, though. There are users I know exist but which are not included in the result. I can query for them individually using this GraphQL query:

query {
  user(username: "someusername") {
    id
  }
}

and receive a result which seems to correctly describe the user.

Why are some users omitted from the results for this query? I know that large result sets require dealing with pagination but the default page size is supposed to be 100 and I am receiving fewer results than this. Additionally, if I request pageinfo and ask for the users after the resulting endCursor I receive no items.

How do I submit a query that gets me all users? Failing that, how do I submit a query that will get me all users which could be assignees for a a group (or, failing that, for a list of projects)?

like image 876
Jean-Paul Calderone Avatar asked Sep 11 '20 13:09

Jean-Paul Calderone


1 Answers

From the documentation :

By default, GitLab’s GraphQL API will return only the first 100 records of any collection. This can be changed by using first or last arguments. Both arguments take a value, so first: 10 will return the first 10 records, and last: 10 the last 10 records.

So you have to do the pagination yourself, your first query would be for example :

query {                                                                                                                                                                                       
  users(first: 10) {
    edges {
      node {                                                                                                                                                                                   
        id                                                                                                                                                                                      
        name                                                                                                                                                                                    
        username                                                                                                                                                                                
      } 
    }
    pageInfo {
      endCursor
      hasNextPage
    }
  }
}

Then use the PageInfo result values to know if you have more pages to fetch and the cursor ID to fetch the next page, for example you could get the following result :

...
"pageInfo": {
   "endCursor": "eyJpZCI6Ijc****************zNjk0MDUwMDAgOVRDIn0",
   "hasNextPage": true
}

So for the next page, you have to query :

query {                                                                                                                                                                                       
  users(first: 10, after: "eyJpZCI6IjcxMj**********************Ni4zNjk0MDUwMDAgOVRDIn0") {
    edges {
      node {                                                                                                                                                                                   
        id                                                                                                                                                                                      
        name                                                                                                                                                                                    
        username                                                                                                                                                                                
      } 
    }
    pageInfo {
      endCursor
      hasNextPage
    }
  }
}

And so on until hasNextPage returns false.

For more info about pagination and cursors, see GraphQL documentation : https://graphql.org/learn/pagination/

like image 108
Yann39 Avatar answered Oct 19 '22 20:10

Yann39