Created
February 14, 2017 16:22
-
-
Save abhiaiyer91/2bf353ee8994372195854cb8c3d0e00b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing gist! I had a small question. How does one pass
_id
to the mutate handler andlabel
to theMutationButton
.I am new to recompose.js. Found it last night actually!