summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/runner/graphql/list/local_state.js
blob: e87bc72c86a86ca952a192e01a6a9175d907b2eb (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
import { makeVar } from '@apollo/client/core';
import typeDefs from './typedefs.graphql';

/**
 * Local state for checkable runner items.
 *
 * Usage:
 *
 * ```
 * import { createLocalState } from '~/runner/graphql/list/local_state';
 *
 * // initialize local state
 * const { cacheConfig, typeDefs, localMutations } = createLocalState();
 *
 * // configure the client
 * apolloClient = createApolloClient({}, { cacheConfig, typeDefs });
 *
 * // modify local state
 * localMutations.setRunnerChecked( ... )
 * ```
 *
 * Note: Currently only in use behind a feature flag:
 * admin_runners_bulk_delete for the admin list, rollout issue:
 * https://gitlab.com/gitlab-org/gitlab/-/issues/353981
 *
 * @returns {Object} An object to configure an Apollo client:
 * contains cacheConfig, typeDefs, localMutations.
 */
export const createLocalState = () => {
  const checkedRunnerIdsVar = makeVar({});

  const cacheConfig = {
    typePolicies: {
      Query: {
        fields: {
          checkedRunnerIds() {
            return Object.entries(checkedRunnerIdsVar())
              .filter(([, isChecked]) => isChecked)
              .map(([key]) => key);
          },
        },
      },
    },
  };

  const localMutations = {
    setRunnerChecked({ runner, isChecked }) {
      checkedRunnerIdsVar({
        ...checkedRunnerIdsVar(),
        [runner.id]: isChecked,
      });
    },
    clearChecked() {
      checkedRunnerIdsVar({});
    },
  };

  return {
    cacheConfig,
    typeDefs,
    localMutations,
  };
};