Commit d6f92dc5 authored by Alexander Turinske's avatar Alexander Turinske

Update apollo mutations to be immutable

- use immer library
parent 4fd889b1
<script>
import { GlButton } from '@gitlab/ui';
import produce from 'immer';
import getProjects from 'ee/security_dashboard/graphql/get_projects.query.graphql';
import projectsQuery from 'ee/security_dashboard/graphql/get_instance_security_dashboard_projects.query.graphql';
import addProjectToSecurityDashboard from 'ee/security_dashboard/graphql/add_project_to_security_dashboard.mutation.graphql';
......@@ -77,12 +78,20 @@ export default {
return;
}
const data = store.readQuery({ query: projectsQuery });
const sourceData = store.readQuery({ query: projectsQuery });
const newProject = results.addProjectToSecurityDashboard.project;
data.instanceSecurityDashboard.projects.nodes.push({
const data = produce(sourceData, draftData => {
// eslint-disable-next-line no-param-reassign
draftData.instanceSecurityDashboard.projects.nodes = [
...draftData.instanceSecurityDashboard.projects.nodes,
{
...newProject,
vulnerabilitySeveritiesCount: newProject.vulnerabilitySeveritiesCount || null, // This is required to surpress missing field warning in GraphQL.
vulnerabilitySeveritiesCount: newProject.vulnerabilitySeveritiesCount || null,
},
];
});
store.writeQuery({ query: projectsQuery, data });
},
})
......@@ -145,12 +154,15 @@ export default {
mutation: deleteProjectFromSecurityDashboard,
variables: { id },
update(store) {
const data = store.readQuery({
query: projectsQuery,
});
data.instanceSecurityDashboard.projects.nodes = data.instanceSecurityDashboard.projects.nodes.filter(
const sourceData = store.readQuery({ query: projectsQuery });
const data = produce(sourceData, draftData => {
// eslint-disable-next-line no-param-reassign
draftData.instanceSecurityDashboard.projects.nodes = draftData.instanceSecurityDashboard.projects.nodes.filter(
curr => curr.id !== id,
);
});
store.writeQuery({ query: projectsQuery, data });
},
})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment