Created
August 10, 2017 17:36
-
-
Save aofleejay/7717b3d06d1c39884dd7e3d8c2010068 to your computer and use it in GitHub Desktop.
Apollo-client mutation request handling
This file contains hidden or 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
class Gallery extends Component { | |
state = { | |
loading: false, | |
} | |
createImageAndReFetch = () => { | |
this.setState({ loading: true }) | |
this.props.createImage({ | |
refetchQueries: [{ query }] | |
}) | |
.then(response => { | |
this.setState({ loading: false }) | |
}) | |
.catch(error => { | |
console.error(error) | |
this.setState({ loading: false }) | |
}) | |
} | |
render() { | |
const { loading, error, allImages } = this.props.data | |
if (loading) return <Loading /> | |
else if (error) return <Error /> | |
return ( | |
<div> | |
{ | |
allImages.map(image => <img key={image.id} src={image.url} />) | |
} | |
<div> | |
<Button | |
type="primary" | |
loading={this.state.loading} | |
onClick={this.createImageAndReFetch} | |
> | |
Load more... | |
</Button> | |
</div> | |
</div> | |
) | |
} | |
} | |
export default compose( | |
graphql(mutation, { name: 'createImage' }), | |
graphql(query) | |
)(Gallery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment