Last active
February 16, 2019 21:48
-
-
Save hwillson/9ff3811d88e439d9fe8d01a4065ae580 to your computer and use it in GitHub Desktop.
Apollo Client - 2.5.0 Announcement Post - Code Splitting - Stats
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 { ApolloConsumer, Query } from 'react-apollo'; | |
import gql from 'graphql-tag'; | |
const GET_MESSAGE_COUNT = gql` | |
{ | |
messageCount @client { | |
total | |
} | |
} | |
`; | |
const resolvers = { | |
Query: { | |
messageCount: (_, args, { cache }) => { | |
// ... calculate and return the number of messages in | |
// the cache ... | |
return { | |
total: 123, | |
__typename: 'MessageCount', | |
}; | |
}, | |
}, | |
}; | |
const MessageCount = () => { | |
return ( | |
<ApolloConsumer> | |
{(client) => { | |
client.addResolvers(resolvers); | |
return ( | |
<Query query={GET_MESSAGE_COUNT}> | |
{({ loading, data: { messageCount } }) => { | |
if (loading) return 'Loading ...'; | |
return ( | |
<p> | |
Total number of messages: {messageCount.total} | |
</p> | |
); | |
}} | |
</Query> | |
); | |
}} | |
</ApolloConsumer> | |
); | |
}; | |
export default MessageCount; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment