Commit 69bdc477 authored by Enrique Alcantara's avatar Enrique Alcantara

Create submit content changes client-side resolver

Wrap submit content changes REST-based service
with an ApolloClient resolver in order to replace
Vuex calls to submit changes with VueApollo
parent 9806da56
......@@ -2,8 +2,8 @@ import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createDefaultClient from '~/lib/graphql';
import typeDefs from './typedefs.graphql';
import fileResolver from './resolvers/file';
import submitContentChangesResolver from './resolvers/submit_content_changes';
Vue.use(VueApollo);
......@@ -13,6 +13,9 @@ const createApolloProvider = appData => {
Project: {
file: fileResolver,
},
Mutation: {
submitContentChanges: submitContentChangesResolver,
},
},
{
typeDefs,
......
mutation submitContentChanges($project: ID!, $username: String!, $sourcePath: String!, $content: String!) {
submitContentChanges(project: $project, username: $username, sourcePath: $sourcePath, content: $content) @client {
branch
commit
mergeRequest
}
}
......@@ -3,6 +3,7 @@ query appData {
isSupportedContent
project
sourcePath
username,
returnUrl
}
}
import submitContentChanges from '../../services/submit_content_changes';
import savedContentMetaQuery from '../queries/saved_content_meta.query.graphql';
const submitContentChangesResolver = (
_root,
{ project: projectId, username, sourcePath, content },
{ cache },
) => {
return submitContentChanges({ projectId, username, sourcePath, content }).then(
savedContentMeta => {
cache.writeQuery({
query: savedContentMetaQuery,
data: {
savedContentMeta: {
__typename: 'SavedContentMeta',
...savedContentMeta,
},
},
});
return null;
},
);
};
export default submitContentChangesResolver;
......@@ -3,6 +3,17 @@ type File {
content: String!
}
type SavedContentField {
label: String!
url: String!
}
type SavedContentMeta {
mergeRequest: SavedContentField!
commit: SavedContentField!
branch: SavedContentField!
}
extend type Project {
file(path: ID!): File
}
......@@ -17,4 +28,9 @@ type AppData {
extend type Query {
appData: AppData!
savedContentMeta: SavedContentMeta
}
extend type Mutation {
submitContentChanges(project: String!, sourcePath: String!, content: String!, username: String!): SavedContentMeta
}
import savedContentMetaQuery from '~/static_site_editor/graphql/queries/saved_content_meta.query.graphql';
import submitContentChanges from '~/static_site_editor/services/submit_content_changes';
import submitContentChangesResolver from '~/static_site_editor/graphql/resolvers/submit_content_changes';
import {
projectId as project,
sourcePath,
username,
sourceContent as content,
savedContentMeta,
} from '../../mock_data';
jest.mock('~/static_site_editor/services/submit_content_changes', () => jest.fn());
describe('static_site_editor/graphql/resolvers/submit_content_changes', () => {
it('writes savedContentMeta query with the data returned by the submitContentChanges service', () => {
const cache = { writeQuery: jest.fn() };
submitContentChanges.mockResolvedValueOnce(savedContentMeta);
return submitContentChangesResolver(
{},
{ path: sourcePath, project, sourcePath, content, username },
{ cache },
).then(() => {
expect(cache.writeQuery).toHaveBeenCalledWith({
query: savedContentMetaQuery,
data: {
savedContentMeta: {
__typename: 'SavedContentMeta',
...savedContentMeta,
},
},
});
});
});
});
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