0
0
GraphqlComparisonBeginner · 4 min read

Apollo vs urql: Key Differences and When to Use Each

Apollo and urql are popular GraphQL clients for JavaScript. Apollo offers a full-featured, robust ecosystem with advanced caching and tooling, while urql is lightweight, flexible, and easier to start with for simple use cases.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of Apollo and urql based on key factors.

FactorApollourql
SizeLarger, more dependenciesSmaller, lightweight
CachingAdvanced normalized cachingBasic document caching with optional normalized cache
Setup ComplexityMore setup and configurationSimple and minimal setup
FeaturesRich features: subscriptions, state management, devtoolsCore features with optional extensions
Community & EcosystemLarge, mature, many integrationsSmaller but growing community
Learning CurveSteeper for beginnersGentle and beginner-friendly
⚖️

Key Differences

Apollo is a comprehensive GraphQL client that provides a powerful normalized cache, which means it stores data in a way that avoids duplication and keeps your UI in sync efficiently. It also includes built-in support for advanced features like subscriptions, local state management, and developer tools, making it suitable for complex applications.

On the other hand, urql is designed to be lightweight and flexible. It uses a simpler caching strategy by default but allows adding normalized caching through optional exchanges. Its minimal setup and modular architecture make it easier to customize and integrate into projects that don't need all the bells and whistles Apollo offers.

In terms of ecosystem, Apollo has a larger community and more official tools, which can be helpful for teams needing extensive support and integrations. Urql’s smaller size and simplicity make it a great choice for projects prioritizing speed and ease of use.

⚖️

Code Comparison

Here is how you fetch data with Apollo Client using React.

javascript
import { ApolloClient, InMemoryCache, ApolloProvider, useQuery, gql } from '@apollo/client';
import React from 'react';

const client = new ApolloClient({
  uri: 'https://graphql-pokemon2.vercel.app/',
  cache: new InMemoryCache()
});

const GET_POKEMON = gql`
  query GetPokemon($name: String!) {
    pokemon(name: $name) {
      id
      number
      name
      attacks {
        special {
          name
          damage
        }
      }
    }
  }
`;

function Pokemon({ name }) {
  const { loading, error, data } = useQuery(GET_POKEMON, {
    variables: { name }
  });

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

  return (
    <div>
      <h2>{data.pokemon.name} (#{data.pokemon.number})</h2>
      <ul>
        {data.pokemon.attacks.special.map((attack) => (
          <li key={attack.name}>{attack.name}: {attack.damage}</li>
        ))}
      </ul>
    </div>
  );
}

export default function App() {
  return (
    <ApolloProvider client={client}>
      <Pokemon name="Pikachu" />
    </ApolloProvider>
  );
}
Output
<div><h2>Pikachu (#025)</h2><ul><li>Discharge: 35</li><li>Thunderbolt: 55</li><li>Wild Charge: 90</li></ul></div>
↔️

urql Equivalent

Here is how you fetch the same data with urql using React.

javascript
import { createClient, Provider, useQuery } from 'urql';
import React from 'react';

const client = createClient({
  url: 'https://graphql-pokemon2.vercel.app/'
});

const GET_POKEMON = `
  query GetPokemon($name: String!) {
    pokemon(name: $name) {
      id
      number
      name
      attacks {
        special {
          name
          damage
        }
      }
    }
  }
`;

function Pokemon({ name }) {
  const [result] = useQuery({
    query: GET_POKEMON,
    variables: { name }
  });
  const { data, fetching, error } = result;

  if (fetching) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;

  return (
    <div>
      <h2>{data.pokemon.name} (#{data.pokemon.number})</h2>
      <ul>
        {data.pokemon.attacks.special.map((attack) => (
          <li key={attack.name}>{attack.name}: {attack.damage}</li>
        ))}
      </ul>
    </div>
  );
}

export default function App() {
  return (
    <Provider value={client}>
      <Pokemon name="Pikachu" />
    </Provider>
  );
}
Output
<div><h2>Pikachu (#025)</h2><ul><li>Discharge: 35</li><li>Thunderbolt: 55</li><li>Wild Charge: 90</li></ul></div>
🎯

When to Use Which

Choose Apollo when building complex applications that need advanced caching, subscriptions, local state management, and a rich ecosystem of tools and integrations. It is ideal for teams that want a mature, battle-tested solution with extensive community support.

Choose urql when you want a lightweight, flexible client with minimal setup and a smaller bundle size. It works well for simpler projects or when you want to customize your GraphQL client behavior with modular exchanges.

Key Takeaways

Apollo offers advanced caching and a rich feature set for complex apps.
urql is lightweight and easier to set up for simpler use cases.
Apollo has a larger ecosystem and community support.
urql provides modularity and flexibility with a smaller bundle size.
Choose based on project complexity and team needs.