Commit 761dd17c authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera Committed by Natalia Tepluhina

Immutable cache update in `submit_content_changes` resolver

parent a7248d86
...@@ -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