Skip to content

Add GraphQL query for deployments (basics)

Shinya Maeda requested to merge graphql-query-for-deployments into master

What does this MR do and why?

This MR adds GraphQL query for fetching deployment information. It adds the following changes:

  • Add DeploymentType that can be fetched under EnvironmentType. In this MR, it can only fetch basic data, such as id and ref.
  • Add DeploymentsResolver to fetch deployments of environments.
  • Extend DeploymentsFinder to filter by multiple statuses, e.g. WHERE status IN (success, failed, canceled).
  • Extend DeploymentsFinder to filter by environment ID, e.g. WHERE environment_id = 123.
  • Adjust InefficientQueryError guard to prevent inefficient parameter combinations.

A few notes:

Query example

Get deployments of the environment

project(fullPath: "shinya.maeda/pipeline-playground") {
  environment(name: "production") {
    deployments {
      nodes {
        id
        iid
        ref
        tag
        sha
        createdAt
        updatedAt
        finishedAt
        status
      }
    }
  }
}

Get the last deployment of the environment

project(fullPath: "shinya.maeda/pipeline-playground") {
  environment(name: "production") {
    deployments(statuses: [SUCCESS], orderBy: { finishedAt: DESC }, first: 1) {
      nodes {
        iid
      }
    }
  }
}

Get the latest upcoming deployment of the environment

project(fullPath: "shinya.maeda/pipeline-playground") {
  environment(name: "production") {
    deployments(statuses: [CREATED RUNNING BLOCKED], orderBy: { createdAt: DESC }, first: 1) {
      nodes {
        iid
      }
    }
  }
}

Related #368680 (closed)

Describe in detail what your merge request does and why.

Screenshots or screen recordings

sample query
{
  project(fullPath: "root/pipeline-playground") {
    environment(name: "prod") {
      deployments {
        nodes {
          id
          iid
          ref
          tag
          sha
          createdAt
          updatedAt
          finishedAt
          status
        }
      }
    }
  }
}
sample response
{
  "data": {
    "project": {
      "environment": {
        "deployments": {
          "nodes": [
            {
              "id": "gid://gitlab/Deployment/90",
              "iid": "11",
              "ref": "main",
              "tag": false,
              "sha": "4dce7b48d7c0250fd80ff5b132112c9af10b0c62",
              "createdAt": "2022-07-28T05:39:23Z",
              "updatedAt": "2022-07-29T07:00:51Z",
              "finishedAt": "2022-07-29T07:00:07Z",
              "status": "FAILED"
            },
            {
              "id": "gid://gitlab/Deployment/89",
              "iid": "10",
              "ref": "feaw-tweatwe",
              "tag": false,
              "sha": "06ec69c77c71caf0a8594d972fc3e4579e5a0bb7",
              "createdAt": "2022-07-08T06:37:33Z",
              "updatedAt": "2022-07-08T06:37:57Z",
              "finishedAt": "2022-07-08T06:37:57Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/88",
              "iid": "9",
              "ref": "fea-tgag",
              "tag": false,
              "sha": "1cd485021b332d5c2a15c52e39b3ae659c6ff69f",
              "createdAt": "2022-07-07T10:07:55Z",
              "updatedAt": "2022-07-07T10:08:00Z",
              "finishedAt": "2022-07-07T10:08:00Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/87",
              "iid": "8",
              "ref": "main",
              "tag": false,
              "sha": "4dce7b48d7c0250fd80ff5b132112c9af10b0c62",
              "createdAt": "2022-07-07T09:45:47Z",
              "updatedAt": "2022-07-07T09:45:50Z",
              "finishedAt": "2022-07-07T09:45:50Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/86",
              "iid": "7",
              "ref": "main",
              "tag": false,
              "sha": "31fc4de87a659e793ff58ec0fc5543e9a14a49cf",
              "createdAt": "2022-07-07T09:35:37Z",
              "updatedAt": "2022-07-07T09:35:41Z",
              "finishedAt": "2022-07-07T09:35:41Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/85",
              "iid": "6",
              "ref": "main",
              "tag": false,
              "sha": "42481a0e3f2a74913e7882e8032fb5aa4f1000c7",
              "createdAt": "2022-07-07T09:32:13Z",
              "updatedAt": "2022-07-07T09:32:32Z",
              "finishedAt": "2022-07-07T09:32:32Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/54",
              "iid": "5",
              "ref": "main",
              "tag": false,
              "sha": "0eb9c39a1b4ef86b46bcb7a1680039637d2ff619",
              "createdAt": "2022-07-07T06:51:36Z",
              "updatedAt": "2022-07-07T06:51:40Z",
              "finishedAt": "2022-07-07T06:51:40Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/53",
              "iid": "4",
              "ref": "fe-asdfdasf",
              "tag": false,
              "sha": "c673e64ec1f05a68994063a16e8ced6ed8ecae8f",
              "createdAt": "2022-07-07T06:50:23Z",
              "updatedAt": "2022-07-07T06:50:28Z",
              "finishedAt": "2022-07-07T06:50:28Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/52",
              "iid": "3",
              "ref": "fe-asdfdasf",
              "tag": false,
              "sha": "df3849416927bc5adaee0769f2f1d6bb7d573866",
              "createdAt": "2022-07-07T06:49:05Z",
              "updatedAt": "2022-07-07T06:49:07Z",
              "finishedAt": "2022-07-07T06:49:07Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/51",
              "iid": "2",
              "ref": "main",
              "tag": false,
              "sha": "eb9974dab65530ef95310e7890230b8450c78d13",
              "createdAt": "2022-07-07T06:46:13Z",
              "updatedAt": "2022-07-07T06:46:16Z",
              "finishedAt": "2022-07-07T06:46:16Z",
              "status": "SUCCESS"
            },
            {
              "id": "gid://gitlab/Deployment/50",
              "iid": "1",
              "ref": "main",
              "tag": false,
              "sha": "875b424809585e5f078dcec2032f74a83c78f960",
              "createdAt": "2022-07-07T06:38:58Z",
              "updatedAt": "2022-07-07T06:42:04Z",
              "finishedAt": "2022-07-07T06:42:04Z",
              "status": "SUCCESS"
            }
          ]
        }
      }
    }
  }
}

Database query example with multiple statuses

SELECT "deployments".* FROM "deployments" WHERE "deployments"."environment_id" = 1178942 AND "deployments"."status" IN (2, 3, 4) ORDER BY "deployments"."id" ASC LIMIT 100

Query plan

How to set up and validate locally

Access to GraphiQL and try execute the above queries.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Shinya Maeda

Merge request reports

Loading