Skip to content

Instantly share code, notes, and snippets.

@abhiaiyer91
Created February 14, 2017 16:22
Show Gist options
  • Save abhiaiyer91/2bf353ee8994372195854cb8c3d0e00b to your computer and use it in GitHub Desktop.
Save abhiaiyer91/2bf353ee8994372195854cb8c3d0e00b to your computer and use it in GitHub Desktop.
import React from 'react';
import { compose, withHandlers } from 'recompose';
import { injectIntl } from 'react-intl';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
type MutationButtonType = {
toggleMutation: Function,
label: React.Element<any>,
};
function MutationButton({ toggleMutation, label }: MutationButtonType): React.Element<any> {
return (
<button onClick={toggleMutation}>
{label}
</button>
);
}
const mutation = gql`
mutation toggleMutation($someId: String!) {
toggle(someId: $someId)
}
`;
export default compose(
injectIntl,
graphql(mutation),
withHandlers({
toggleMutation: ({ mutate, _id, intl }: { mutate: Function, _id: string, intl: Object }): Function => {
return (): Promise<any> => {
return mutate({
variables: {
someId: _id,
},
}).then((data: string) => {
console.log(data, 'Return value');
}).catch((e: Object) => {
console.error(e, 'Error');
});
};
}
})
)(MutationButton);
@younisshah
Copy link

Amazing gist! I had a small question. How does one pass _id to the mutate handler and label to the MutationButton.
I am new to recompose.js. Found it last night actually!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment