Skip to content

Gitlab graphql API

Prasad Talasila edited this page Sep 3, 2023 · 6 revisions

Fetch list of files and directories

query fileslist {
  project(fullPath: "gitlab-org/gitlab-foss") {
    repository {
      tree(recursive: true) {
        blobs {
          edges {
            node {
              type
              name
              path
            }
          }
        }
        trees {
          edges {
            node {
              type
              path
              name
            }
          }
        }
      }
    }
  }
}

Exploration of a directory path

query directorylist {
  project(fullPath: "gitlab-org/gitlab-foss") {
    webUrl
    path
    repository {
      paginatedTree(path: ".gitlab/ci", recursive: true) {
        nodes {
          trees {
            nodes {
              path
            }
          }
        }
      }
      diskPath
    }
  }
}


query directorylist {
  project(fullPath: "gitlab-org/gitlab-foss") {
    webUrl
    path
    repository {
      paginatedTree(path: "config", recursive: true) {
        nodes {
          trees {
            nodes {
              path
            }
          }
        }
      }
      diskPath
    }
  }
}

recursive: true is an optional argument. If this argument is not given, only the contents of the directory are given.

Fetch the contents of a file

query filecontents {
  project(fullPath: "gitlab-org/gitlab-foss") {
    repository {
      blobs(paths: [".codeclimate.yml"]) {
        nodes {
          name
          rawBlob
          rawTextBlob
        }
      }
    }
  }
}

Note

Quite a few microservices shall use gitlab graphql schema. It would be better to use gitlab graphql schema and make the code using schema first approach.

Use the tutorial here to get complete gitlab graphql schema from schema explorer

A Bug

GitLab GraphQL Schema has a bug. Apparently, the id of a tree entry cannot be expected to have a unique identifier despite the documentation.

In query

query gitLabDirectoryListQuery(
  $path: String!
  $groupAndProject: ID!
) {
  project(fullPath: $groupAndProject) {
    repository {
      paginatedTree(path: $path, recursive: false) {
        nodes {
          trees {
            nodes {
              name
              path
              id
            }
          }
        }
      }
    }
  }
}

with variables

{
  "path": "Data",
  "groupAndProject" : "dtaas/user1",
}

We get this response:

{
  "data": {
    "project": {
      "repository": {
        "paginatedTree": {
          "nodes": [
            {
              "trees": {
                "nodes": [
                  {
                    "name": "MyDopeData",
                    "path": "Data/MyDopeData",
                    "id": "gid://gitlab/Gitlab::Graphql::Representation::TreeEntry/f93e3a1a1525fb5b91020da86e44810c87a2d7bc"
                  },
                  {
                    "name": "OtherData",
                    "path": "Data/OtherData",
                    "id": "gid://gitlab/Gitlab::Graphql::Representation::TreeEntry/f93e3a1a1525fb5b91020da86e44810c87a2d7bc"
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

You would expect the id of a node to be unique, but it may not be so.

The entries for dtaas and dtaas1 have the same ID, which should be impossible. The GitLab team knows this.

For a context and setup to recreate the problem, please see PR-78

References

  1. gitlab graphql api, sample project for queries
  2. gitlab REST API for files