Day 5 of 5
⏱ ~60 minutes
GraphQL in 5 Days — Day 5

Apollo Client

Connect a React app to your GraphQL API with Apollo Client, queries, mutations, and caching.

Setting Up Apollo Client

Terminal
npm install @apollo/client graphql
src/apolloClient.js
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';

const httpLink = createHttpLink({ uri: 'http://localhost:4000/graphql' });

const authLink = setContext((_, { headers }) => {
  const token = localStorage.getItem('token');
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : '',
    }
  };
});

export const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
});
Wrap App with ApolloProvider
import { ApolloProvider } from '@apollo/client';
import { client } from './apolloClient';

function App() {
  return (
    
      
    
  );
}
useQuery and useMutation
import { useQuery, useMutation, gql } from '@apollo/client';

const GET_USERS = gql`
  query GetUsers {
    users { id name email }
  }
`;

const CREATE_USER = gql`
  mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) { id name email }
  }
`;

function UserList() {
  const { loading, error, data } = useQuery(GET_USERS);
  const [createUser] = useMutation(CREATE_USER, {
    refetchQueries: [GET_USERS],  // refetch after mutation
  });

  if (loading) return 

Loading...

; if (error) return

Error: {error.message}

; return (
{data.users.map(u =>
{u.name}
)}
); }
📝 Day 5 Exercise
Build a Full-Stack GraphQL App
  1. C
  2. o
  3. n
  4. n
  5. e
  6. c
  7. t
  8. a
  9. R
  10. e
  11. a
  12. c
  13. t
  14. a
  15. p
  16. p
  17. t
  18. o
  19. y
  20. o
  21. u
  22. r
  23. G
  24. r
  25. a
  26. p
  27. h
  28. Q
  29. L
  30. s
  31. e
  32. r
  33. v
  34. e
  35. r
  36. w
  37. i
  38. t
  39. h
  40. A
  41. p
  42. o
  43. l
  44. l
  45. o
  46. C
  47. l
  48. i
  49. e
  50. n
  51. t
  52. .
  53. D
  54. i
  55. s
  56. p
  57. l
  58. a
  59. y
  60. t
  61. h
  62. e
  63. u
  64. s
  65. e
  66. r
  67. s
  68. l
  69. i
  70. s
  71. t
  72. w
  73. i
  74. t
  75. h
  76. u
  77. s
  78. e
  79. Q
  80. u
  81. e
  82. r
  83. y
  84. .
  85. A
  86. d
  87. d
  88. a
  89. f
  90. o
  91. r
  92. m
  93. t
  94. h
  95. a
  96. t
  97. c
  98. r
  99. e
  100. a
  101. t
  102. e
  103. s
  104. a
  105. u
  106. s
  107. e
  108. r
  109. w
  110. i
  111. t
  112. h
  113. u
  114. s
  115. e
  116. M
  117. u
  118. t
  119. a
  120. t
  121. i
  122. o
  123. n
  124. a
  125. n
  126. d
  127. r
  128. e
  129. f
  130. e
  131. t
  132. c
  133. h
  134. e
  135. s
  136. t
  137. h
  138. e
  139. l
  140. i
  141. s
  142. t
  143. o
  144. n
  145. s
  146. u
  147. c
  148. c
  149. e
  150. s
  151. s
  152. .

Day 5 Summary

  • Apollo Client wraps your app in ApolloProvider and makes the client available everywhere.
  • useQuery(QUERY) returns { loading, error, data }. Handles fetching automatically.
  • useMutation(MUTATION) returns a function to call when ready, plus state.
  • refetchQueries in useMutation tells Apollo to re-run specific queries after the mutation completes.
Finished this lesson?