mirror of
https://github.com/vale981/Vulcan
synced 2025-03-09 04:16:37 -04:00
47 lines
No EOL
926 B
JavaScript
47 lines
No EOL
926 B
JavaScript
/*
|
|
|
|
HoC that provides a simple mutation that expects a single JSON object in return
|
|
|
|
Example usage:
|
|
|
|
export default withMutation({
|
|
name: 'getEmbedData',
|
|
args: {url: 'String'},
|
|
})(EmbedURL);
|
|
|
|
*/
|
|
|
|
import { graphql } from 'react-apollo';
|
|
import gql from 'graphql-tag';
|
|
|
|
export default function withMutation({name, args}) {
|
|
|
|
let mutation;
|
|
|
|
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}) {
|
|
${name}(${args2})
|
|
}
|
|
`
|
|
} else {
|
|
mutation = `
|
|
mutation ${name} {
|
|
${name}
|
|
}
|
|
`
|
|
}
|
|
|
|
return graphql(gql`${mutation}`, {
|
|
alias: 'withMutation',
|
|
props: ({ownProps, mutate}) => ({
|
|
[name]: (vars) => {
|
|
return mutate({
|
|
variables: vars,
|
|
});
|
|
}
|
|
}),
|
|
});
|
|
} |