Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resolvers.js « graphql « deploy_keys « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8693665b90c14445e1cfa91079f84f6ae947108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { gql } from '@apollo/client/core';
import axios from '~/lib/utils/axios_utils';
import {
  convertObjectPropsToCamelCase,
  parseIntPagination,
  normalizeHeaders,
} from '~/lib/utils/common_utils';
import pageInfoQuery from '~/graphql_shared/client/page_info.query.graphql';
import currentPageQuery from './queries/current_page.query.graphql';
import currentScopeQuery from './queries/current_scope.query.graphql';
import confirmRemoveKeyQuery from './queries/confirm_remove_key.query.graphql';

export const mapDeployKey = (deployKey) => ({
  ...convertObjectPropsToCamelCase(deployKey, { deep: true }),
  __typename: 'LocalDeployKey',
});

const DEFAULT_PAGE_SIZE = 5;

export const resolvers = (endpoints) => ({
  Project: {
    deployKeys(_, { scope, page }, { client }) {
      const key = `${scope}Endpoint`;
      let { [key]: endpoint } = endpoints;

      if (!endpoint) {
        endpoint = endpoints.enabledKeysEndpoint;
      }

      return axios
        .get(endpoint, { params: { page, per_page: DEFAULT_PAGE_SIZE } })
        .then(({ headers, data }) => {
          const normalizedHeaders = normalizeHeaders(headers);
          const pageInfo = {
            ...parseIntPagination(normalizedHeaders),
            __typename: 'LocalPageInfo',
          };
          client.writeQuery({
            query: pageInfoQuery,
            variables: { input: { page, scope } },
            data: { pageInfo },
          });
          return data?.keys?.map(mapDeployKey) || [];
        });
    },
  },
  Mutation: {
    currentPage(_, { page }, { client }) {
      client.writeQuery({
        query: currentPageQuery,
        data: { currentPage: page },
      });
    },
    currentScope(_, { scope }, { client }) {
      const key = `${scope}Endpoint`;
      const { [key]: endpoint } = endpoints;

      if (!endpoint) {
        throw new Error(`invalid deploy key scope selected: ${scope}`);
      }

      client.writeQuery({
        query: currentPageQuery,
        data: { currentPage: 1 },
      });
      client.writeQuery({
        query: currentScopeQuery,
        data: { currentScope: scope },
      });
    },
    disableKey(_, _variables, { client }) {
      const {
        deployKeyToRemove: { id },
      } = client.readQuery({
        query: confirmRemoveKeyQuery,
      });

      const fragment = gql`
        fragment DisablePath on LocalDeployKey {
          disablePath
        }
      `;

      const { disablePath } = client.readFragment({ fragment, id: `LocalDeployKey:${id}` });

      return axios.put(disablePath).then(({ data }) => {
        client.cache.evict({ fieldName: 'deployKeyToRemove' });
        client.cache.evict({ id: `LocalDeployKey:${id}` });
        client.cache.gc();

        return data;
      });
    },
    enableKey(_, { id }, { client }) {
      const fragment = gql`
        fragment EnablePath on LocalDeployKey {
          enablePath
        }
      `;

      const { enablePath } = client.readFragment({ fragment, id: `LocalDeployKey:${id}` });

      return axios.put(enablePath).then(({ data }) => {
        client.cache.evict({ id: `LocalDeployKey:${id}` });
        client.cache.gc();

        return data;
      });
    },
    confirmDisable(_, { id }, { client }) {
      client.writeQuery({
        query: confirmRemoveKeyQuery,
        data: { deployKeyToRemove: id ? { id, __type: 'LocalDeployKey' } : null },
      });
    },
  },
});