Guest User

Untitled

a guest
Feb 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import React from 'react';
  2. import { ApolloConsumer, Query } from 'react-apollo';
  3. import gql from 'graphql-tag';
  4.  
  5. const GET_MESSAGE_COUNT = gql`
  6. {
  7. messageCount @client {
  8. total
  9. }
  10. }
  11. `;
  12.  
  13. const resolvers = {
  14. Query: {
  15. messageCount: (_, args, { cache }) => {
  16. // ... calculate and return the number of messages in
  17. // the cache ...
  18. return {
  19. total: 123,
  20. __typename: 'MessageCount',
  21. };
  22. },
  23. },
  24. };
  25.  
  26. const MessageCount = () => {
  27. return (
  28. <ApolloConsumer>
  29. {(client) => {
  30. client.addResolvers(resolvers);
  31. return (
  32. <Query query={GET_MESSAGE_COUNT}>
  33. {({ loading, data: { messageCount } }) => {
  34. if (loading) return 'Loading ...';
  35. return (
  36. <p>
  37. Total number of messages: {messageCount.total}
  38. </p>
  39. );
  40. }}
  41. </Query>
  42. );
  43. }}
  44. </ApolloConsumer>
  45. );
  46. };
  47.  
  48. export default MessageCount;
Add Comment
Please, Sign In to add comment