Commit 3ab22b71 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'nmezzopera-submit-content-changes-immer' into 'master'

Immutable cache update in `submit_content_changes` resolver

See merge request gitlab-org/gitlab!44929
parents f89853ea 761dd17c
...@@ -21,6 +21,7 @@ const createApolloProvider = appData => { ...@@ -21,6 +21,7 @@ const createApolloProvider = appData => {
}, },
{ {
typeDefs, typeDefs,
assumeImmutableResults: true,
}, },
); );
......
import { produce } from 'immer';
import query from '../queries/app_data.query.graphql'; import query from '../queries/app_data.query.graphql';
const hasSubmittedChangesResolver = (_, { input: { hasSubmittedChanges } }, { cache }) => { const hasSubmittedChangesResolver = (_, { input: { hasSubmittedChanges } }, { cache }) => {
const { appData } = cache.readQuery({ query }); const oldData = cache.readQuery({ query });
cache.writeQuery({
query, const data = produce(oldData, draftState => {
data: { // punctually modifying draftState as per immer docs upsets our linters
return {
...draftState,
appData: { appData: {
__typename: 'AppData', __typename: 'AppData',
...appData, ...draftState.appData,
hasSubmittedChanges, hasSubmittedChanges,
}, },
}, };
});
cache.writeQuery({
query,
data,
}); });
}; };
......
import { produce } from 'immer';
import submitContentChanges from '../../services/submit_content_changes'; import submitContentChanges from '../../services/submit_content_changes';
import savedContentMetaQuery from '../queries/saved_content_meta.query.graphql'; import savedContentMetaQuery from '../queries/saved_content_meta.query.graphql';
...@@ -14,14 +15,18 @@ const submitContentChangesResolver = ( ...@@ -14,14 +15,18 @@ const submitContentChangesResolver = (
images, images,
mergeRequestMeta, mergeRequestMeta,
}).then(savedContentMeta => { }).then(savedContentMeta => {
cache.writeQuery({ const data = produce(savedContentMeta, draftState => {
query: savedContentMetaQuery, return {
data: {
savedContentMeta: { savedContentMeta: {
__typename: 'SavedContentMeta', __typename: 'SavedContentMeta',
...savedContentMeta, ...draftState,
}, },
}, };
});
cache.writeQuery({
query: savedContentMetaQuery,
data,
}); });
}); });
}; };
......
import appDataQuery from '~/static_site_editor/graphql/queries/app_data.query.graphql';
import hasSubmittedChanges from '~/static_site_editor/graphql/resolvers/has_submitted_changes';
describe('static_site_editor/graphql/resolvers/has_submitted_changes', () => {
it('updates the cache with the data passed in input', () => {
const cachedData = { appData: { original: 'foo' } };
const newValue = { input: { hasSubmittedChanges: true } };
const cache = {
readQuery: jest.fn().mockReturnValue(cachedData),
writeQuery: jest.fn(),
};
hasSubmittedChanges(null, newValue, { cache });
expect(cache.readQuery).toHaveBeenCalledWith({ query: appDataQuery });
expect(cache.writeQuery).toHaveBeenCalledWith({
query: appDataQuery,
data: {
appData: {
__typename: 'AppData',
original: 'foo',
hasSubmittedChanges: true,
},
},
});
});
});
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