Created
February 16, 2019 21:27
-
-
Save hwillson/c6ad5171b032e424c58f734c074dff9e to your computer and use it in GitHub Desktop.
Apollo Client - 2.5.0 Announcement Post - Code Splitting 3
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 100; | |
}, | |
}, | |
}; | |
const MessageCount = () => { | |
return ( | |
<ApolloConsumer> | |
{(client) => { | |
client.addResolvers(resolvers); | |
return ( | |
<Query query={GET_MESSAGE_COUNT} fetchPolicy="no-cache"> | |
{({ 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