React Apollo Integration (Deprecated)
React Apollo (package `react-apollo`) provided the official React integration for Apollo Client, enabling developers to fetch and manage GraphQL data in React applications using Hooks, Components, and Higher-Order Components (HOCs). Version 3.1.5, released on April 14, 2020, was a stable release within the v3 series. The project has since been officially deprecated, with version 4.0.0 (released July 20, 2020) being its final release. All core React Apollo functionality, including hooks, components, HOCs, SSR, and testing utilities, has been migrated directly into the `@apollo/client` package (v3 and above). Developers are strongly advised to migrate to `@apollo/client` for active maintenance, new features, and bug fixes, as `react-apollo` no longer receives updates or support.
Common errors
-
Error: Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>.
cause The ApolloClient instance was not provided to the React context, usually because the root component is not wrapped with `<ApolloProvider />` or the client prop is missing.fixEnsure your application's root component (or the highest common ancestor of your GraphQL components) is wrapped in `<ApolloProvider client={yourApolloClientInstance}>...</ApolloProvider>`. -
TypeError: Cannot destructure property 'data' of 'undefined' or 'null'.
cause This often happens when `useQuery` returns `undefined` for `data` (or `loading`/`error` properties) before the query has completed or if `useQuery` itself is returning `undefined` as reported in issue #4042 for v3.x.fixAlways check for `loading` and `error` states before trying to access `data`. For specific issues like #4042, consider upgrading or downgrading `react-apollo` to a working version or, preferably, migrating to `@apollo/client`. -
GraphQL error: Network error: Failed to fetch
cause The client could not connect to the GraphQL server, often due to an incorrect URI, network issues, or CORS policy blocks.fixVerify the `uri` in your `HttpLink` configuration is correct and accessible. Check browser console for CORS errors. Ensure your GraphQL server is running and reachable. -
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application.
cause A GraphQL operation (query, mutation, subscription) completes after the component that initiated it has unmounted, leading to state updates on an unmounted component.fixUse a ref to track component mounted status or implement cleanup logic (e.g., in `useEffect` for hooks, `componentWillUnmount` for classes) to cancel pending operations or prevent state updates if the component is unmounted. -
Unhandled Rejection (Error): A breaking change in 3.1.5 affects useLazyQuery
cause Specific breaking changes in `useLazyQuery` were reported for `react-apollo` 3.1.5, causing unexpected behavior or errors.fixReview the usage of `useLazyQuery` in your application. Consult GitHub issues (e.g., #4040) for details on the breaking change. Consider pinning to an earlier `react-apollo` 3.x version or migrating to `@apollo/client` which provides its own `useLazyQuery` hook.
Warnings
- breaking The `react-apollo` package is officially deprecated and archived. Version 4.0.0 was the final release. All future development, bug fixes, and features are integrated directly into the `@apollo/client` package (version 3 and above). Continuing to use `react-apollo` means using an unsupported library.
- breaking Version 3.0.0 introduced significant breaking changes, including a minimum React version requirement of 16.8. The `react-apollo` package primarily re-exports functionality from `@apollo/react-hooks`, `@apollo/react-components`, and `@apollo/react-hoc`. Testing utilities were moved to `@apollo/react-testing`.
- breaking A breaking change was introduced in `useLazyQuery` in versions 3.1.5, potentially altering its behavior or API.
- gotcha Versions 3.1.4 and 3.1.5 were reported to break `addMocksToSchema` functionality from `@graphql-tools/mock` when used with `MockedProvider`.
- gotcha Using `setState` within the `onError` or `onCompleted` callbacks of the `Query` component (and potentially `useQuery` hook) could lead to infinite loops in earlier versions (2.5.3). This pattern can still be problematic if not carefully managed.
Install
-
npm install react-apollo -
yarn add react-apollo -
pnpm add react-apollo
Imports
- ApolloProvider
const ApolloProvider = require('react-apollo').ApolloProvider;import { ApolloProvider } from 'react-apollo'; - Query
import Query from 'react-apollo/Query';
import { Query } from 'react-apollo'; - useQuery
import { useQuery } from '@apollo/react-hooks';import { useQuery } from 'react-apollo'; - graphql
import graphql from 'react-apollo/lib/graphql';
import { graphql } from 'react-apollo';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloClient, InMemoryCache, HttpLink } from 'apollo-client';
import { ApolloProvider, useQuery, gql } from 'react-apollo';
// Create an Apollo Client instance
const client = new ApolloClient({
link: new HttpLink({
uri: 'https://graphqlzero.almansi.me/api',
}),
cache: new InMemoryCache(),
});
// Define your GraphQL query
const GET_TODOS = gql`
query GetTodos {
todos(options: { limit: 5 }) {
data {
id
title
completed
}
}
}
`;
// A React component that uses the useQuery hook
function Todos() {
const { loading, error, data } = useQuery(GET_TODOS);
if (loading) return <p>Loading todos...</p>;
if (error) return <p>Error: {error.message}</p>;
return (
<div>
<h2>My Todos (via react-apollo)</h2>
<ul>
{data.todos.data.map(({ id, title, completed }) => (
<li key={id} style={{ textDecoration: completed ? 'line-through' : 'none' }}>
{title}
</li>
))}
</ul>
</div>
);
}
// Render the application wrapped in ApolloProvider
ReactDOM.render(
<ApolloProvider client={client}>
<Todos />
</ApolloProvider>,
document.getElementById('root')
);