Created
August 12, 2018 19:03
-
-
Save marcaaron/6e2b3451cf25c7ec8a5b209cd9f7be19 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 { gql } from 'apollo-boost'; | |
import { withApollo } from 'react-apollo'; | |
class Query extends Component { | |
state = { | |
data: null, | |
loading: true | |
} | |
async componentDidMount(){ | |
const { query, client } = this.props; | |
const clientQuery = await client.query({query}); | |
this.setState({...this.state, ...clientQuery}); | |
} | |
render(){ | |
return this.props.children(this.state); | |
} | |
} | |
const FakeQuery = withApollo(Query); | |
const MyComponent = (props) => { | |
return ( | |
<FakeQuery | |
query={MY_QUERY}> | |
{ ({loading, data}) => { | |
if(loading) return 'Loading'; | |
const { things } = data; | |
return ( | |
<div> | |
{things.map(({id, text})=><p key={id}>{text}</p>)} | |
</div> | |
); | |
}} | |
</FakeQuery> | |
) | |
} | |
const MY_QUERY = gql` | |
query { | |
things{ | |
id | |
text | |
} | |
} | |
`; | |
export default MyComponent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment