🌐 Web Development

Apollo

Apollo GraphQL

A GraphQL client/server library. Integrates with React and Node.js.

📖 Detailed Explanation

Apollo is the most widely used client and server library in the GraphQL ecosystem. Apollo Client integrates with various frontends such as React, Vue, and Angular, and minimizes network requests with a normalized caching system. Apollo Server makes it easy to build GraphQL APIs in a Node.js environment.

A project started by Meteor Development Group in 2016, it is now operated by Apollo GraphQL, an independent company. It is used by large companies such as GitHub, Airbnb, and Expedia, and has become the de facto standard tool for adopting GraphQL. Apollo Studio, the enterprise version, supports schema management, performance monitoring, and federated graphs.

The core of Apollo Client is InMemoryCache. It normalizes query results based on __typename and id before storing them, and when the same data is requested, it returns it immediately from the cache instead of the network. It also integrates naturally with React through the useQuery, useMutation, and useSubscription hooks.

In practice, Apollo shines when integrating GraphQL APIs from multiple microservices (Federation) or in applications with complex data relationships. Optimistic UI updates allow the UI to reflect changes immediately during a mutation, and even local state can be managed with GraphQL.

💻 Code Example

// Apollo Client 설정 및 사용법
import { ApolloClient, InMemoryCache, gql, useQuery, useMutation } from '@apollo/client';

// 클라이언트 설정
const client = new ApolloClient({
  uri: 'https://api.example.com/graphql',
  cache: new InMemoryCache({
    typePolicies: {
      User: {
        keyFields: ['id'],
      },
    },
  }),
});

// 쿼리 정의
const GET_USERS = gql`
  query GetUsers($limit: Int!) {
    users(limit: $limit) {
      id
      name
      email
      avatar
    }
  }
`;

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

// React 컴포넌트에서 사용
function UserList() {
  const { loading, error, data, refetch } = useQuery(GET_USERS, {
    variables: { limit: 10 },
    fetchPolicy: 'cache-and-network',
  });

  const [createUser] = useMutation(CREATE_USER, {
    // Optimistic UI 업데이트
    optimisticResponse: {
      createUser: {
        __typename: 'User',
        id: 'temp-id',
        name: '새 사용자',
        email: 'new@example.com',
      },
    },
    // 캐시 업데이트
    update(cache, { data: { createUser } }) {
      const existing = cache.readQuery({ query: GET_USERS, variables: { limit: 10 } });
      cache.writeQuery({
        query: GET_USERS,
        variables: { limit: 10 },
        data: { users: [...existing.users, createUser] },
      });
    },
  });

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error.message}</p>;

  return (
    <ul>
      {data.users.map(user => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
}

🗣️ Say It Like This in Practice

💬 In a Meeting
"We cut 3 REST API calls down to a single GraphQL query with Apollo Client, and the initial load improved from 800ms to 200ms. Caching is automatic too, so when we request the same data again, it shows up right away without going over the network."
💬 In an Interview
"Apollo Client does normalized caching with InMemoryCache. It identifies entities by the combination of __typename and id, and when you update something in one place, it is reflected in all queries that reference that data. You can choose strategies such as cache-first and network-only with fetchPolicy."
💬 In a code review
"I think we should add optimisticResponse to this mutation. The UI updates immediately without waiting for the server response, so the perceived speed gets much faster. And if it fails, Apollo automatically rolls it back."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Using it without cache keys

If you query without an id field, cache normalization does not work. Include id in every type, or specify keyFields in typePolicies.

❌
Leaving the N+1 query problem unaddressed

If you write resolvers in Apollo Server without DataLoader, the N+1 problem occurs. Always use DataLoader for batch processing.

✅
The right pattern

Always include id and __typename in your queries, and update the cache after a mutation with refetchQueries or cache.modify.

🔗 Related Terms

📚 Learn More