debian-mirror-gitlab/app/assets/javascripts/repository/graphql.js

67 lines
2 KiB
JavaScript
Raw Normal View History

2019-09-04 21:01:54 +05:30
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
2019-12-26 22:10:19 +05:30
import axios from '~/lib/utils/axios_utils';
2019-09-04 21:01:54 +05:30
import createDefaultClient from '~/lib/graphql';
import introspectionQueryResultData from './fragmentTypes.json';
2019-09-30 21:07:59 +05:30
import { fetchLogsTree } from './log_tree';
2019-09-04 21:01:54 +05:30
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(
2019-09-30 21:07:59 +05:30
{
Query: {
commit(_, { path, fileName, type }) {
return new Promise(resolve => {
fetchLogsTree(defaultClient, path, '0', {
resolve,
entry: {
name: fileName,
type,
},
});
});
},
2019-12-26 22:10:19 +05:30
readme(_, { url }) {
return axios
2020-07-28 23:09:34 +05:30
.get(url, { params: { format: 'json', viewer: 'rich' } })
2019-12-26 22:10:19 +05:30
.then(({ data }) => ({ ...data, __typename: 'ReadmeFile' }));
},
2019-09-30 21:07:59 +05:30
},
},
2019-09-04 21:01:54 +05:30
{
cacheConfig: {
fragmentMatcher,
dataIdFromObject: obj => {
2020-04-22 19:07:51 +05:30
/* eslint-disable @gitlab/require-i18n-strings */
2019-09-04 21:01:54 +05:30
// 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':
2020-04-22 19:07:51 +05:30
return `${encodeURIComponent(obj.flatPath)}-${obj.id}`;
2019-09-04 21:01:54 +05:30
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;
}
2020-04-22 19:07:51 +05:30
/* eslint-enable @gitlab/require-i18n-strings */
2019-09-04 21:01:54 +05:30
},
},
},
);
export default new VueApollo({
defaultClient,
});