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 => {
},
{
typeDefs,
assumeImmutableResults: true,
},
);
......
import { produce } from 'immer';
import query from '../queries/app_data.query.graphql';
const hasSubmittedChangesResolver = (_, { input: { hasSubmittedChanges } }, { cache }) => {
const { appData } = cache.readQuery({ query });
cache.writeQuery({
query,
data: {
const oldData = cache.readQuery({ query });
const data = produce(oldData, draftState => {
// punctually modifying draftState as per immer docs upsets our linters
return {
...draftState,
appData: {
__typename: 'AppData',
...appData,
...draftState.appData,
hasSubmittedChanges,
},
},
};
});
cache.writeQuery({
query,
data,
});
};
......
import { produce } from 'immer';
import submitContentChanges from '../../services/submit_content_changes';
import savedContentMetaQuery from '../queries/saved_content_meta.query.graphql';
......@@ -14,14 +15,18 @@ const submitContentChangesResolver = (
images,
mergeRequestMeta,
}).then(savedContentMeta => {
cache.writeQuery({
query: savedContentMetaQuery,
data: {
const data = produce(savedContentMeta, draftState => {
return {
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