2016-12-06 16:25:50 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
HoC that provides a simple mutation that expects a single JSON object in return
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
export default withMutation({
|
2017-05-13 16:36:01 +09:00
|
|
|
name: 'getEmbedData',
|
2016-12-06 16:25:50 +09:00
|
|
|
args: {url: 'String'},
|
2017-09-15 10:05:18 +02:00
|
|
|
})(EmbedURL);
|
2016-12-06 16:25:50 +09:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2018-06-20 10:24:30 +09:00
|
|
|
import { getFragment } from 'meteor/vulcan:lib';
|
2016-12-06 16:25:50 +09:00
|
|
|
|
2018-06-20 10:24:30 +09:00
|
|
|
export default function withMutation({name, args, fragmentName}) {
|
2016-12-06 16:25:50 +09:00
|
|
|
|
2018-06-20 10:24:30 +09:00
|
|
|
let mutation, fragment, fragmentBlock = '';
|
2017-01-20 14:08:45 +09:00
|
|
|
|
2018-06-20 10:24:30 +09:00
|
|
|
if (fragmentName) {
|
|
|
|
fragment = getFragment(fragmentName);
|
|
|
|
fragmentBlock = `{
|
|
|
|
...${fragmentName}
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
|
2017-01-20 14:08:45 +09:00
|
|
|
if (args) {
|
|
|
|
const args1 = _.map(args, (type, name) => `$${name}: ${type}`); // e.g. $url: String
|
|
|
|
const args2 = _.map(args, (type, name) => `${name}: $${name}`); // e.g. $url: url
|
|
|
|
mutation = `
|
|
|
|
mutation ${name}(${args1}) {
|
2018-06-20 10:24:30 +09:00
|
|
|
${name}(${args2})${fragmentBlock}
|
2017-01-20 14:08:45 +09:00
|
|
|
}
|
|
|
|
`
|
|
|
|
} else {
|
|
|
|
mutation = `
|
|
|
|
mutation ${name} {
|
2018-06-20 10:24:30 +09:00
|
|
|
${name}${fragmentBlock}
|
2017-01-20 14:08:45 +09:00
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
2018-07-04 10:59:10 +02:00
|
|
|
|
|
|
|
return graphql(gql`${mutation}${fragmentName ? fragment : ''}`, {
|
2017-06-06 17:22:17 -07:00
|
|
|
alias: 'withMutation',
|
2016-12-07 15:30:40 +09:00
|
|
|
props: ({ownProps, mutate}) => ({
|
|
|
|
[name]: (vars) => {
|
|
|
|
return mutate({
|
|
|
|
variables: vars,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
2016-12-06 16:25:50 +09:00
|
|
|
});
|
|
|
|
}
|