Last active
September 18, 2016 21:12
-
-
Save jbaxleyiii/70c5fff8fadfe25ef7ad4d5e623d92e3 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, { Component } from 'react'; | |
import { graphql } from 'react-apollo'; | |
import gql from 'graphql-tag'; | |
const GET_USER_WITH_ID = gql` | |
query getUser(id: $ID!) { | |
user { name } | |
} | |
`; | |
const withUser = graphql(GET_USER_WITH_ID, { | |
// `ownProps` are the props passed into `MyComponentWithData` | |
// `data` is the result data (see above) | |
props: ({ ownProps, data }) => { | |
if (data.loading) return { userLoading: true }; | |
if (data.error) return { hasErrors: true }; | |
return { | |
currentUser: data.user, | |
refetchUser: data.refetch, | |
}; | |
} | |
}); | |
class MyComponent extends Component { ... } | |
const MyComponentWithData = withUser(MyComponent); | |
MyComponent.propTypes = { | |
userLoading: React.PropTypes.boolean, | |
hasErrors: React.PropTypes.boolean, | |
currentUser: React.PropTypes.object, | |
refetchUser: React.PropTypes.func, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment