How to select a list of projects from GitLab?

Viewed 1716

I just started to learn GraphQL (and GitLab GraphQL API through their GraphiQL Browser tool). But currently I cannot achieve anything successful - I am trying to make the simplest query, to select a list of projects. Let's say a list of their names. And I tried different syntax in the style query { projects } and similar without success. Would somebody show me this simple query - to pass some quickstart and also if it's possible - a link to the documentation about such kind of queries, because I found only snippet like:

query {
  project(fullPath: "gitlab-org/graphql-sandbox") {
    name
    issues {
      nodes {
        title
        description
      }
    }
  }
}

so to get the idea - where to look for relevant info in the API documentation for similar questions.

2 Answers

You can request a list of project belonging to a group using group(fullPath: "groupName"):

{
  group(fullPath: "gitlab-org") {
    projects(first: 100) {
      nodes {
        name
        issues {
          nodes {
            title
            description
          }
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}

On gitlab.com, you can search for projects using the projects field with search parameter, at this time, this is not working on Gitlab CE :

{
  projects(search: "test", first: 100) {
    nodes {
      name
    }
    pageInfo {
      endCursor
      hasNextPage
    }
  }
}

Note that in GraphiQL Browser you can use autocomplete with ctrl + space which is very useful to find fields:

autocomplete field

Also when you press ctrl + click on a field, it shows the documentation on the right :

enter image description here

You have a great training on graphql on graphql.org learn section which is very interesting

First things first: you have to know which version is the gitlab you want to query.

Gitlab.org is usually using the most recent available version.

According to this version you can read the corresponding documentation by selecting the appropriate value in the version dropdown box

version dropdown box screenshot

As it has been already pointed out, newest versions implement projects root level query. My company current server version, which is 12.10, does not, it only expose this root level queries:

  1. project : Project information, with many of its associations such as issues and merge requests
  2. group : Basic group information and epics are currently supported.
  3. user : Information about a particular user.
  4. namespace : Within a namespace it is also possible to fetch projects.
  5. currentUser: Information about the currently logged in user.
  6. users: Information about a collection of users.
  7. metaData: Metadata about GitLab and the GraphQL API.
  8. snippets: Snippets visible to the currently logged in user.

To answer the question, a query that returns a list of project names, working on all versions of gitlab exposing GraphQL endpoint, is the following (in this case the name of the projects belonging to a common group)

query {
  group(fullPath: "your-group/your-subgroup") {
    projects {
      nodes {
        name
      }
    }
  }
}

NOTE: the question is only about GraphQL APIs but the question title does not specify this so, given also REST APIs are available, this can lead to confusion. Better to include "GraphQL" somewhere in the title, in my opinion.

Related