summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repository/graphql.js
blob: 6cb253c8169e012021692925e99a20bcfd91284c (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
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
import createDefaultClient from '~/lib/graphql';
import introspectionQueryResultData from './fragmentTypes.json';
import { fetchLogsTree } from './log_tree';

Vue.use(VueApollo);

// We create a fragment matcher so that we can create a fragment from an interface
// Without this, Apollo throws a heuristic fragment matcher warning
const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData,
});

const defaultClient = createDefaultClient(
  {
    Query: {
      commit(_, { path, fileName, type }) {
        return new Promise(resolve => {
          fetchLogsTree(defaultClient, path, '0', {
            resolve,
            entry: {
              name: fileName,
              type,
            },
          });
        });
      },
    },
  },
  {
    cacheConfig: {
      fragmentMatcher,
      dataIdFromObject: obj => {
        /* eslint-disable @gitlab/i18n/no-non-i18n-strings */
        // eslint-disable-next-line no-underscore-dangle
        switch (obj.__typename) {
          // We need to create a dynamic ID for each entry
          // Each entry can have the same ID as the ID is a commit ID
          // So we create a unique cache ID with the path and the ID
          case 'TreeEntry':
          case 'Submodule':
          case 'Blob':
            return `${obj.flatPath}-${obj.id}`;
          default:
            // If the type doesn't match any of the above we fallback
            // to using the default Apollo ID
            // eslint-disable-next-line no-underscore-dangle
            return obj.id || obj._id;
        }
        /* eslint-enable @gitlab/i18n/no-non-i18n-strings */
      },
    },
  },
);

export default new VueApollo({
  defaultClient,
});