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

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { uniqueId } from 'lodash';
2020-11-24 15:15:51 +05:30
import produce from 'immer';
2020-05-24 23:13:21 +05:30
import { defaultDataIdFromObject } from 'apollo-cache-inmemory';
2020-11-24 15:15:51 +05:30
import axios from '~/lib/utils/axios_utils';
2020-05-24 23:13:21 +05:30
import createDefaultClient from '~/lib/graphql';
import activeDiscussionQuery from './graphql/queries/active_discussion.query.graphql';
2020-11-24 15:15:51 +05:30
import getDesignQuery from './graphql/queries/get_design.query.graphql';
2020-05-24 23:13:21 +05:30
import typeDefs from './graphql/typedefs.graphql';
2020-11-24 15:15:51 +05:30
import { extractTodoIdFromDeletePath, createPendingTodo } from './utils/design_management_utils';
import { CREATE_DESIGN_TODO_EXISTS_ERROR } from './utils/error_messages';
import { addPendingTodoToStore } from './utils/cache_update';
2020-05-24 23:13:21 +05:30
Vue.use(VueApollo);
const resolvers = {
Mutation: {
updateActiveDiscussion: (_, { id = null, source }, { cache }) => {
2020-11-24 15:15:51 +05:30
const sourceData = cache.readQuery({ query: activeDiscussionQuery });
const data = produce(sourceData, draftData => {
// eslint-disable-next-line no-param-reassign
draftData.activeDiscussion = {
__typename: 'ActiveDiscussion',
id,
source,
};
});
2020-05-24 23:13:21 +05:30
cache.writeQuery({ query: activeDiscussionQuery, data });
},
2020-11-24 15:15:51 +05:30
createDesignTodo: (
_,
{ projectPath, issueId, designId, issueIid, filenames, atVersion },
{ cache },
) => {
return axios
.post(`/${projectPath}/todos`, {
issue_id: issueId,
issuable_id: designId,
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-24 23:13:21 +05:30
},
};
const defaultClient = createDefaultClient(
resolvers,
// 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: {
dataIdFromObject: object => {
// 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);
},
},
typeDefs,
2020-11-24 15:15:51 +05:30
assumeImmutableResults: true,
2020-05-24 23:13:21 +05:30
},
);
export default new VueApollo({
defaultClient,
});