Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
Add Comment
Please, Sign In to add comment