2016-11-26 11:17:43 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Generic mutation wrapper to remove a document from a collection.
|
|
|
|
|
|
|
|
Sample mutation:
|
|
|
|
|
|
|
|
mutation moviesRemove($documentId: String) {
|
|
|
|
moviesEdit(documentId: $documentId) {
|
|
|
|
...MoviesRemoveFormFragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
- documentId: the id of the document to remove
|
|
|
|
|
|
|
|
Child Props:
|
|
|
|
|
|
|
|
- removeMutation(documentId)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
import update from 'immutability-helper';
|
|
|
|
|
|
|
|
export default function withRemove(options) {
|
|
|
|
|
2016-12-01 16:12:39 +01:00
|
|
|
const { collection, fragment, queryToUpdate } = options,
|
|
|
|
fragmentName = fragment.definitions[0].name.value,
|
2016-11-26 11:17:43 +09:00
|
|
|
mutationName = collection.options.mutations.remove.name,
|
|
|
|
listResolverName = collection.options.resolvers.list.name,
|
|
|
|
totalResolverName = collection.options.resolvers.total.name;
|
|
|
|
|
|
|
|
return graphql(gql`
|
|
|
|
mutation ${mutationName}($documentId: String) {
|
|
|
|
${mutationName}(documentId: $documentId) {
|
|
|
|
...${fragmentName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
|
|
|
props: ({ ownProps, mutate }) => ({
|
|
|
|
removeMutation: ({ documentId }) => {
|
|
|
|
|
2016-11-30 10:31:19 +01:00
|
|
|
// if a queryToUpdate is passed as prop, generate updateQueries function
|
|
|
|
let updateQueries = {};
|
|
|
|
if (queryToUpdate) {
|
|
|
|
updateQueries = {
|
|
|
|
[queryToUpdate]: (prev, { mutationResult }) => {
|
|
|
|
// filter the list to get a new one without the document
|
|
|
|
const listWithoutDocument = prev[listResolverName].filter(doc => doc._id !== documentId);
|
|
|
|
// update the query
|
|
|
|
const newList = update(prev, {
|
|
|
|
[listResolverName]: { $set: listWithoutDocument }, // ex: postsList
|
|
|
|
[totalResolverName]: { $set: prev.postsListTotal - 1 } // ex: postsListTotal
|
|
|
|
});
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
};
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
2016-11-30 10:31:19 +01:00
|
|
|
|
|
|
|
console.log('// removeMutation')
|
|
|
|
console.log(updateQueries);
|
|
|
|
|
2016-11-26 11:17:43 +09:00
|
|
|
return mutate({
|
|
|
|
variables: { documentId },
|
2016-11-30 10:31:19 +01:00
|
|
|
updateQueries: options.updateQueries || updateQueries
|
|
|
|
});
|
2016-11-26 11:17:43 +09:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|