React Apollo Integration (Deprecated)

3.1.5 · deprecated · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `react-apollo` with `ApolloClient`, define a GraphQL query using `gql`, and fetch data in a React component using the `useQuery` hook. It renders a list of todos from a public API.

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')
);

view raw JSON →