2016-11-26 11:17:43 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Generic mutation wrapper to edit a document in a collection.
|
|
|
|
|
|
|
|
Sample mutation:
|
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
mutation updateMovie($documentId: String, $set: MoviesInput, $unset: MoviesUnset) {
|
|
|
|
updateMovie(documentId: $documentId, set: $set, unset: $unset) {
|
|
|
|
...MovieFormFragment
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
- documentId: the id of the document to modify
|
|
|
|
- set: an object containing all the fields to modify and their new values
|
|
|
|
- unset: an object containing the fields to unset
|
|
|
|
|
|
|
|
Child Props:
|
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
- updateMovie(documentId, set, unset)
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2018-05-10 17:48:22 +09:00
|
|
|
import { getFragment, getFragmentName, getCollection } from 'meteor/vulcan:lib';
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
export default function withUpdate(options) {
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-01-27 19:11:03 -07:00
|
|
|
const { collectionName } = options;
|
|
|
|
// get options
|
|
|
|
const collection = options.collection || getCollection(collectionName),
|
2017-02-03 15:16:48 +09:00
|
|
|
fragment = options.fragment || getFragment(options.fragmentName),
|
|
|
|
fragmentName = getFragmentName(fragment),
|
2018-06-02 18:49:53 +09:00
|
|
|
typeName = collection.options.typeName;
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
return graphql(gql`
|
2018-06-04 11:22:49 +09:00
|
|
|
mutation update${typeName}($documentId: String, $set: ${collection.options.collectionName}Input, $unset: ${collection.options.collectionName}Unset) {
|
|
|
|
update${typeName}(documentId: $documentId, set: $set, unset: $unset) {
|
2016-11-26 11:17:43 +09:00
|
|
|
...${fragmentName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
2018-06-02 18:49:53 +09:00
|
|
|
alias: `withUpdate${typeName}`,
|
2016-11-26 11:17:43 +09:00
|
|
|
props: ({ ownProps, mutate }) => ({
|
2018-06-02 18:49:53 +09:00
|
|
|
[`update${typeName}`]: (args) => {
|
|
|
|
const { documentId, set, unset } = args;
|
|
|
|
return mutate({
|
|
|
|
variables: { documentId, set, unset }
|
|
|
|
// note: updateQueries is not needed for editing documents
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// OpenCRUD backwards compatibility
|
2016-11-26 11:17:43 +09:00
|
|
|
editMutation: (args) => {
|
|
|
|
const { documentId, set, unset } = args;
|
|
|
|
return mutate({
|
|
|
|
variables: { documentId, set, unset }
|
|
|
|
// note: updateQueries is not needed for editing documents
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|