2022-02-14 07:14:02 -05:00
|
|
|
import { defaultDataIdFromObject } from '@apollo/client/core';
|
2021-02-14 13:09:20 -05:00
|
|
|
import produce from 'immer';
|
|
|
|
import { uniqueId } from 'lodash';
|
2020-05-07 08:09:46 -04:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import createDefaultClient from '~/lib/graphql';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-05-15 02:08:40 -04:00
|
|
|
import activeDiscussionQuery from './graphql/queries/active_discussion.query.graphql';
|
2020-09-09 05:08:40 -04:00
|
|
|
import getDesignQuery from './graphql/queries/get_design.query.graphql';
|
2020-05-15 02:08:40 -04:00
|
|
|
import typeDefs from './graphql/typedefs.graphql';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { addPendingTodoToStore } from './utils/cache_update';
|
2020-09-09 05:08:40 -04:00
|
|
|
import { extractTodoIdFromDeletePath, createPendingTodo } from './utils/design_management_utils';
|
|
|
|
import { CREATE_DESIGN_TODO_EXISTS_ERROR } from './utils/error_messages';
|
2020-05-07 08:09:46 -04:00
|
|
|
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
2020-05-15 02:08:40 -04:00
|
|
|
const resolvers = {
|
|
|
|
Mutation: {
|
|
|
|
updateActiveDiscussion: (_, { id = null, source }, { cache }) => {
|
2020-08-25 08:04:30 -04:00
|
|
|
const sourceData = cache.readQuery({ query: activeDiscussionQuery });
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const data = produce(sourceData, (draftData) => {
|
2020-08-25 08:04:30 -04:00
|
|
|
draftData.activeDiscussion = {
|
|
|
|
__typename: 'ActiveDiscussion',
|
|
|
|
id,
|
|
|
|
source,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-15 02:08:40 -04:00
|
|
|
cache.writeQuery({ query: activeDiscussionQuery, data });
|
|
|
|
},
|
2020-09-11 05:08:44 -04:00
|
|
|
createDesignTodo: (
|
|
|
|
_,
|
|
|
|
{ projectPath, issueId, designId, issueIid, filenames, atVersion },
|
|
|
|
{ cache },
|
|
|
|
) => {
|
2020-09-09 05:08:40 -04:00
|
|
|
return axios
|
|
|
|
.post(`/${projectPath}/todos`, {
|
|
|
|
issue_id: issueId,
|
2020-09-11 05:08:44 -04:00
|
|
|
issuable_id: designId,
|
2020-09-09 05:08:40 -04:00
|
|
|
issuable_type: 'design',
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
const { delete_path } = data;
|
|
|
|
const todoId = extractTodoIdFromDeletePath(delete_path);
|
|
|
|
if (!todoId) {
|
|
|
|
return {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: CREATE_DESIGN_TODO_EXISTS_ERROR,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingTodo = createPendingTodo(todoId);
|
|
|
|
addPendingTodoToStore(cache, pendingTodo, getDesignQuery, {
|
|
|
|
fullPath: projectPath,
|
|
|
|
iid: issueIid,
|
|
|
|
filenames,
|
|
|
|
atVersion,
|
|
|
|
});
|
|
|
|
|
|
|
|
return pendingTodo;
|
|
|
|
});
|
|
|
|
},
|
2020-05-15 02:08:40 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-07 08:09:46 -04:00
|
|
|
const defaultClient = createDefaultClient(
|
2020-05-15 02:08:40 -04:00
|
|
|
resolvers,
|
2020-05-07 08:09:46 -04:00
|
|
|
// This config is added temporarily to resolve an issue with duplicate design IDs.
|
|
|
|
// Should be removed as soon as https://gitlab.com/gitlab-org/gitlab/issues/13495 is resolved
|
|
|
|
{
|
|
|
|
cacheConfig: {
|
2020-12-23 16:10:24 -05:00
|
|
|
dataIdFromObject: (object) => {
|
2020-05-07 08:09:46 -04:00
|
|
|
// eslint-disable-next-line no-underscore-dangle, @gitlab/require-i18n-strings
|
|
|
|
if (object.__typename === 'Design') {
|
|
|
|
return object.id && object.image ? `${object.id}-${object.image}` : uniqueId();
|
|
|
|
}
|
|
|
|
return defaultDataIdFromObject(object);
|
|
|
|
},
|
|
|
|
},
|
2020-05-15 02:08:40 -04:00
|
|
|
typeDefs,
|
2020-05-07 08:09:46 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export default new VueApollo({
|
|
|
|
defaultClient,
|
|
|
|
});
|