debian-mirror-gitlab/app/assets/javascripts/graphql_shared/issuable_client.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

244 lines
7.8 KiB
JavaScript
Raw Normal View History

2022-07-23 23:45:48 +05:30
import produce from 'immer';
2021-12-11 22:18:48 +05:30
import VueApollo from 'vue-apollo';
2023-01-13 00:05:48 +05:30
import { defaultDataIdFromObject } from '@apollo/client/core';
2022-10-11 01:57:18 +05:30
import { concatPagination } from '@apollo/client/utilities';
import getIssueStateQuery from '~/issues/show/queries/get_issue_state.query.graphql';
2021-12-11 22:18:48 +05:30
import createDefaultClient from '~/lib/graphql';
2022-10-11 01:57:18 +05:30
import typeDefs from '~/work_items/graphql/typedefs.graphql';
2023-03-17 16:20:25 +05:30
import { WIDGET_TYPE_NOTES } from '~/work_items/constants';
2023-07-09 08:55:56 +05:30
import workItemQuery from '~/work_items/graphql/work_item.query.graphql';
2023-04-23 21:23:45 +05:30
import { findHierarchyWidgetChildren } from '~/work_items/utils';
2023-06-20 00:43:36 +05:30
import activeBoardItemQuery from 'ee_else_ce/boards/graphql/client/active_board_item.query.graphql';
2022-07-23 23:45:48 +05:30
2023-01-13 00:05:48 +05:30
export const config = {
2022-07-23 23:45:48 +05:30
typeDefs,
cacheConfig: {
2023-01-13 00:05:48 +05:30
// included temporarily until Vuex is removed from boards app
dataIdFromObject: (object) => {
// eslint-disable-next-line no-underscore-dangle
2023-04-23 21:23:45 +05:30
return object.__typename === 'BoardList' && !window.gon?.features?.apolloBoards
? object.iid
: defaultDataIdFromObject(object);
2022-07-23 23:45:48 +05:30
},
typePolicies: {
2022-10-11 01:57:18 +05:30
Project: {
fields: {
projectMembers: {
keyArgs: ['fullPath', 'search', 'relations', 'first'],
},
},
},
2023-03-17 16:20:25 +05:30
WorkItemWidgetNotes: {
fields: {
// If we add any key args, the discussions field becomes discussions({"filter":"ONLY_ACTIVITY","first":10}) and
// kills any possibility to handle it on the widget level without hardcoding a string.
discussions: {
keyArgs: false,
},
},
},
WorkItemWidgetProgress: {
fields: {
progress: {
// We want to show null progress as 0 as per https://gitlab.com/gitlab-org/gitlab/-/issues/386117
read(existing) {
return existing === null ? 0 : existing;
},
},
},
},
2022-07-23 23:45:48 +05:30
WorkItem: {
fields: {
2023-03-17 16:20:25 +05:30
// widgets policy because otherwise the subscriptions invalidate the cache
2022-08-27 11:52:29 +05:30
widgets: {
2023-03-17 16:20:25 +05:30
merge(existing = [], incoming, context) {
2022-11-25 23:54:43 +05:30
if (existing.length === 0) {
return incoming;
}
return existing.map((existingWidget) => {
2023-01-13 00:05:48 +05:30
const incomingWidget = incoming.find(
(w) => w.type && w.type === existingWidget.type,
);
2023-03-17 16:20:25 +05:30
// We don't want to override existing notes with empty widget on work item updates
if (incomingWidget?.type === WIDGET_TYPE_NOTES && !context.variables.pageSize) {
return existingWidget;
}
// we want to concat next page of discussions to the existing ones
if (incomingWidget?.type === WIDGET_TYPE_NOTES && context.variables.after) {
// concatPagination won't work because we were placing new widget here so we have to do this manually
return {
...incomingWidget,
discussions: {
...incomingWidget.discussions,
nodes: [
...existingWidget.discussions.nodes,
...incomingWidget.discussions.nodes,
],
},
};
}
2023-04-23 21:23:45 +05:30
2022-11-25 23:54:43 +05:30
return incomingWidget || existingWidget;
});
2022-08-27 11:52:29 +05:30
},
},
2023-06-20 00:43:36 +05:30
userPermissions: {
read(permission = {}) {
return {
...permission,
setWorkItemMetadata: false,
};
},
},
2022-07-23 23:45:48 +05:30
},
},
2022-10-11 01:57:18 +05:30
MemberInterfaceConnection: {
fields: {
nodes: concatPagination(),
},
},
2023-04-23 21:23:45 +05:30
...(window.gon?.features?.apolloBoards
? {
BoardList: {
fields: {
issues: {
keyArgs: ['filters'],
},
},
},
IssueConnection: {
merge(existing = { nodes: [] }, incoming, { args }) {
if (!args.after) {
return incoming;
}
return {
...incoming,
nodes: [...existing.nodes, ...incoming.nodes],
};
},
},
EpicList: {
fields: {
epics: {
keyArgs: ['filters'],
},
},
},
EpicConnection: {
merge(existing = { nodes: [] }, incoming, { args }) {
if (!args.after) {
return incoming;
}
return {
...incoming,
nodes: [...existing.nodes, ...incoming.nodes],
};
},
},
2023-06-20 00:43:36 +05:30
Group: {
fields: {
projects: {
keyArgs: ['includeSubgroups', 'search'],
},
descendantGroups: {
keyArgs: ['includeSubgroups', 'search'],
},
},
},
ProjectConnection: {
fields: {
nodes: concatPagination(),
},
},
GroupConnection: {
fields: {
nodes: concatPagination(),
},
},
Board: {
fields: {
epics: {
keyArgs: ['boardId'],
},
},
},
2023-04-23 21:23:45 +05:30
BoardEpicConnection: {
merge(existing = { nodes: [] }, incoming, { args }) {
if (!args.after) {
return incoming;
}
return {
...incoming,
nodes: [...existing.nodes, ...incoming.nodes],
};
},
},
}
: {}),
2022-07-23 23:45:48 +05:30
},
},
};
export const resolvers = {
Mutation: {
2023-04-23 21:23:45 +05:30
addHierarchyChild: (_, { id, workItem }, { cache }) => {
2023-07-09 08:55:56 +05:30
const queryArgs = { query: workItemQuery, variables: { id } };
2023-04-23 21:23:45 +05:30
const sourceData = cache.readQuery(queryArgs);
const data = produce(sourceData, (draftState) => {
findHierarchyWidgetChildren(draftState.workItem).push(workItem);
});
cache.writeQuery({ ...queryArgs, data });
},
removeHierarchyChild: (_, { id, workItem }, { cache }) => {
2023-07-09 08:55:56 +05:30
const queryArgs = { query: workItemQuery, variables: { id } };
2023-04-23 21:23:45 +05:30
const sourceData = cache.readQuery(queryArgs);
const data = produce(sourceData, (draftState) => {
const hierarchyChildren = findHierarchyWidgetChildren(draftState.workItem);
const index = hierarchyChildren.findIndex((child) => child.id === workItem.id);
hierarchyChildren.splice(index, 1);
});
cache.writeQuery({ ...queryArgs, data });
},
2022-10-11 01:57:18 +05:30
updateIssueState: (_, { issueType = undefined, isDirty = false }, { cache }) => {
const sourceData = cache.readQuery({ query: getIssueStateQuery });
const data = produce(sourceData, (draftData) => {
draftData.issueState = { issueType, isDirty };
});
cache.writeQuery({ query: getIssueStateQuery, data });
},
2023-06-20 00:43:36 +05:30
setActiveBoardItem(_, { boardItem }, { cache }) {
cache.writeQuery({
query: activeBoardItemQuery,
data: { activeBoardItem: boardItem },
});
return boardItem;
},
2023-07-09 08:55:56 +05:30
clientToggleListCollapsed(_, { list = {}, collapsed = false }) {
return {
list: {
...list,
collapsed,
},
};
},
clientToggleEpicListCollapsed(_, { list = {}, collapsed = false }) {
return {
list: {
...list,
collapsed,
},
};
},
2022-07-23 23:45:48 +05:30
},
};
2021-12-11 22:18:48 +05:30
2023-01-13 00:05:48 +05:30
export const defaultClient = createDefaultClient(resolvers, config);
2021-12-11 22:18:48 +05:30
2022-10-11 01:57:18 +05:30
export const apolloProvider = new VueApollo({
defaultClient,
});