Skip to content

Instantly share code, notes, and snippets.

@hwillson
Last active February 16, 2019 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwillson/9ff3811d88e439d9fe8d01a4065ae580 to your computer and use it in GitHub Desktop.
Save hwillson/9ff3811d88e439d9fe8d01a4065ae580 to your computer and use it in GitHub Desktop.
Apollo Client - 2.5.0 Announcement Post - Code Splitting - Stats
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