2016-11-26 11:17:43 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Generic mutation wrapper to remove a document from a collection.
|
|
|
|
|
|
|
|
Sample mutation:
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
mutation deleteMovie($input: DeleteMovieInput) {
|
|
|
|
deleteMovie(input: $input) {
|
|
|
|
data {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
__typename
|
|
|
|
}
|
|
|
|
__typename
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
- input
|
|
|
|
- input.selector: the id of the document to remove
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
Child Props:
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
- deleteMovie({ selector })
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2018-06-05 16:07:38 +09:00
|
|
|
import { getFragment, getFragmentName, getCollection, deleteClientTemplate } from 'meteor/vulcan:core';
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
const withDelete = (options) => {
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-01-27 19:11:03 -07:00
|
|
|
const { collectionName } = options;
|
2018-06-08 13:31:15 +09:00
|
|
|
const collection = options.collection || getCollection(collectionName);
|
|
|
|
const fragment = options.fragment || getFragment(options.fragmentName || `${collectionName}DefaultFragment`);
|
|
|
|
const fragmentName = getFragmentName(fragment);
|
|
|
|
const typeName = collection.options.typeName;
|
|
|
|
const query = gql`${deleteClientTemplate({ typeName, fragmentName })}${fragment}`;
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-06-05 16:07:38 +09:00
|
|
|
return graphql(query, {
|
2018-06-02 18:49:53 +09:00
|
|
|
alias: `withDelete${typeName}`,
|
2016-11-26 11:17:43 +09:00
|
|
|
props: ({ ownProps, mutate }) => ({
|
2018-06-05 16:07:38 +09:00
|
|
|
|
|
|
|
[`delete${typeName}`]: (args) => {
|
|
|
|
const { selector } = args;
|
2018-06-02 18:49:53 +09:00
|
|
|
return mutate({
|
2018-06-05 16:07:38 +09:00
|
|
|
variables: { input: { selector } }
|
2018-06-02 18:49:53 +09:00
|
|
|
});
|
|
|
|
},
|
2018-06-05 16:07:38 +09:00
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
// OpenCRUD backwards compatibility
|
2018-06-05 16:07:38 +09:00
|
|
|
removeMutation: (args) => {
|
|
|
|
const { documentId } = args;
|
|
|
|
const selector = { documentId };
|
2016-11-26 11:17:43 +09:00
|
|
|
return mutate({
|
2018-06-05 16:07:38 +09:00
|
|
|
variables: { input: { selector } }
|
2016-11-30 10:31:19 +01:00
|
|
|
});
|
2016-11-26 11:17:43 +09:00
|
|
|
},
|
2018-06-05 16:07:38 +09:00
|
|
|
|
2016-11-26 11:17:43 +09:00
|
|
|
}),
|
|
|
|
});
|
2018-06-08 13:31:15 +09:00
|
|
|
}
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
export default withDelete;
|