Created
August 21, 2018 19:57
-
-
Save kdipaolo/b1332b3fdd9164f9c389a53b493ea6a1 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
// App.js | |
// Need apollo-boost for link state functionality | |
import ApolloClient from 'apollo-boost' | |
const defaultLinkState = { | |
edit: false, | |
open: false | |
} | |
const client = new ApolloClient({ | |
uri: '<GRAPHQL_API_ENDPOINT>', | |
clientState: { | |
defaults: defaultLinkState, | |
resolvers: {} | |
} | |
}) | |
// Posts.js | |
import React, { Component } from 'react' | |
class Posts extends Component { | |
render() { | |
return ( | |
<Query query={EXAMPLE_POSTS_QUERY}> | |
{({data, loading)) => { | |
return ( | |
<div> | |
<h1>Posts</h1> | |
<Button open={data.open}/> | |
{data.map(post => <p open={data.open}>{post.title}</p>)} | |
</div> | |
}} | |
</Query> | |
) | |
} | |
} | |
const EXAMPLE_POSTS_QUERY = gql` | |
query posts { | |
posts { | |
id | |
title | |
} | |
// Need @client to pull from state | |
edit @client, | |
open @client | |
} | |
` | |
// Toggle.js | |
import React, { Component } from 'react' | |
import {ApolloConsumer} from 'react-apollo' | |
class Button extends Component { | |
render() { | |
const { open } = this.props | |
return ( | |
<ApolloConsumer> | |
{client => | |
<button | |
onClick={() => { | |
client.writeData({data: {open: !open)}) | |
} | |
> | |
Toggle | |
</button> | |
} | |
</ApolloConsumer> | |
) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment