Which is Better GraphQL Client Libraries?
graphql-request vs apollo-client vs urql vs react-apollo
1 Year
graphql-requestapollo-clienturqlreact-apolloSimilar Packages:
What's GraphQL Client Libraries?

GraphQL client libraries are essential tools for managing data fetching and state management in applications that utilize GraphQL APIs. They provide abstractions and utilities to simplify the process of querying data, handling responses, and managing local state. These libraries vary in terms of features, complexity, and design philosophies, catering to different use cases and developer preferences. By leveraging these libraries, developers can enhance their productivity, maintainability, and performance when working with GraphQL.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
graphql-request4,005,9015,842862 kB254 months agoMIT
apollo-client416,55419,362-5164 years agoMIT
urql212,8948,635325 kB26a day agoMIT
react-apollo101,0116,852-2055 years agoMIT
Feature Comparison: graphql-request vs apollo-client vs urql vs react-apollo

Data Fetching

  • graphql-request: GraphQL Request offers a minimalistic API for making GraphQL queries and mutations. It allows developers to send requests with a simple function call, making it easy to integrate into any JavaScript project. However, it lacks advanced features like caching and state management, which may require additional implementation.
  • apollo-client: Apollo Client provides a powerful and flexible API for fetching data from GraphQL endpoints. It supports query batching, pagination, and automatic caching of results, which optimizes network requests and improves performance. Apollo's caching mechanism allows for efficient data retrieval and minimizes redundant requests, making it suitable for complex applications.
  • urql: Urql provides a flexible and efficient way to fetch data from GraphQL APIs. It supports query and mutation execution with a focus on simplicity and performance. Urql's architecture allows for easy customization of the data fetching process, making it a good choice for projects that require specific optimizations.
  • react-apollo: React Apollo integrates Apollo Client with React, providing hooks and components that simplify data fetching within React components. It allows developers to easily manage loading states, error handling, and data updates directly in their UI components, enhancing the developer experience when working with GraphQL.

State Management

  • graphql-request: GraphQL Request does not provide any state management features. It is solely focused on making GraphQL requests, meaning developers will need to implement their own state management solutions if needed, which can increase complexity in larger applications.
  • apollo-client: Apollo Client offers built-in state management capabilities, allowing developers to manage both remote and local state in a unified way. It enables optimistic UI updates and provides tools for managing local cache, making it ideal for applications that require complex state interactions.
  • urql: Urql allows for local state management through its built-in features, but it is more lightweight compared to Apollo Client. Developers can manage local state using Urql's cache and custom hooks, providing flexibility in how state is handled in the application.
  • react-apollo: React Apollo leverages Apollo Client's state management capabilities, allowing developers to manage local state alongside remote data seamlessly. It provides hooks for managing local state and integrating it with the GraphQL data layer, streamlining the development process in React applications.

Learning Curve

  • graphql-request: GraphQL Request has a very low learning curve, as it provides a simple API for making requests. Developers can quickly get started without needing to understand complex concepts, making it an excellent choice for beginners or small projects.
  • apollo-client: Apollo Client has a moderate learning curve due to its extensive features and concepts such as caching, state management, and subscriptions. Developers may need to invest time in understanding these concepts to fully leverage Apollo's capabilities, especially in larger applications.
  • urql: Urql has a relatively low learning curve, especially for developers familiar with React. Its modular design allows developers to learn and implement features incrementally, making it accessible for those who want a flexible solution without overwhelming complexity.
  • react-apollo: React Apollo has a moderate learning curve, particularly for developers already familiar with React. Understanding how to use hooks and manage data within React components may take some time, but the integration with Apollo Client simplifies the process of working with GraphQL in React.

Extensibility

  • graphql-request: GraphQL Request is not designed for extensibility. Its minimalistic approach means that it lacks built-in features for customization, which may limit its use in more complex scenarios where additional functionality is required.
  • apollo-client: Apollo Client is highly extensible, allowing developers to create custom links, middleware, and cache implementations. This flexibility enables developers to tailor the client to their specific needs and integrate it with other libraries and frameworks easily.
  • urql: Urql is designed with extensibility in mind, allowing developers to create custom exchanges to modify the behavior of the client. This modular approach enables developers to add or remove features as needed, making it a versatile choice for various use cases.
  • react-apollo: React Apollo is extensible through its integration with Apollo Client, allowing developers to create custom hooks and components that can enhance functionality. However, it is primarily focused on the React ecosystem, which may limit its use in non-React applications.

Performance

  • graphql-request: GraphQL Request is lightweight and has minimal overhead, making it performant for simple use cases. However, it does not include built-in caching or optimizations, which may lead to increased network requests in larger applications.
  • apollo-client: Apollo Client is optimized for performance with features like intelligent caching and query batching, which reduce the number of network requests and improve loading times. However, its extensive features can lead to increased bundle size, which may impact performance in smaller applications.
  • urql: Urql is designed to be lightweight and performant, with a focus on minimizing the overhead of data fetching. Its customizable architecture allows developers to optimize performance based on their specific requirements, making it a good choice for applications that prioritize speed.
  • react-apollo: React Apollo benefits from Apollo Client's performance optimizations, including caching and batching. It is designed to minimize unnecessary re-renders and efficiently manage data updates, making it suitable for performance-sensitive applications.
How to Choose: graphql-request vs apollo-client vs urql vs react-apollo
  • graphql-request: Choose GraphQL Request for its simplicity and minimalistic approach. It is best suited for small projects or when you need a lightweight solution without the overhead of a full-fledged client. It provides a straightforward API for making GraphQL requests without additional features.
  • apollo-client: Choose Apollo Client if you need a comprehensive solution that includes advanced features like caching, optimistic UI updates, and built-in support for subscriptions. It is ideal for larger applications that require a robust state management solution alongside data fetching.
  • urql: Choose Urql if you prefer a more flexible and modular approach to GraphQL data fetching. It is designed to be lightweight and customizable, allowing you to pick and choose features as needed. Urql is suitable for projects that require a balance between simplicity and extensibility.
  • react-apollo: Choose React Apollo if you are building a React application and want to integrate Apollo Client seamlessly with React components. It offers hooks and higher-order components to manage data fetching and state, making it easier to work with GraphQL in a React context.
README for graphql-request

graphql-request

Minimal GraphQL client supporting Node and browsers for scripts or simple apps.

GitHub Action npm version

Highlights

  • Most simple & lightweight GraphQL client
  • Promise-based API (works with async / await)
  • Pure ESM package
  • First class TypeScript support
    • Including TypedDocumentNode
  • Isomorphic (works in both Node and Browsers)

Install

npm add graphql-request graphql

TypeScript Setup

This package uses package.exports. Therefore if you are a TypeScript user you must:

  1. have your tsconfig.json moduleResolution set to "bundler" or "node16"/"nodenext".
  2. Have your package.json type set to "module".

Quick Start

Send a GraphQL document using a static request function:

import { gql, request } from 'graphql-request'

const document = gql`
  {
    company {
      ceo
    }
  }
`
await request('https://api.spacex.land/graphql/', document)

The function can be passed a configuration object for more complex cases:

await request({
  url,
  document,
  variables,
  requestHeaders,
})

A class is available for constructing your own instances:

import { gql, GraphQLClient } from 'graphql-request'

const document = gql`
  {
    company {
      ceo
    }
  }
`
const endpoint = 'https://api.spacex.land/graphql/'
const client = new GraphQLClient(endpoint)
await client.request(document)

Examples

Node Version Support

We only (officially) support versions of Nodejs of the following status:

  • Current
  • LTS
  • Maintenance and end of life not yet reached

So for example on Oct 24 2023 that would mean these versions: 18, 20, 21.

Any issue that exists solely for an unsupported version of Nodejs will be rejected (not worked on).

Reference

⚠️ This reference is incomplete. Check out the examples for more reference material.

Configuration

ErrorPolicy

By default GraphQLClient will throw when an error is received. However, sometimes you still want to resolve the (partial) data you received. You can define errorPolicy in the GraphQLClient constructor.

const client = new GraphQLClient(endpoint, { errorPolicy: 'all' })
None (default)

Allow no errors at all. If you receive a GraphQL error the client will throw.

Ignore

Ignore incoming errors and resolve like no errors occurred

All

Return both the errors and data, only works with rawRequest.

IgnoreOperationName

OperationName has been introduced to address issues reported here Support operation name, However, on certain occasions this information may not be needed in requests. In such cases, you might consider ignoring operationName to avoid the extraction steps currently performed by a parsing operation when the document is provided in string format.

By default the GraphQLClient tries to extract the operationName from the document. You can define excludeOperationName in the constructor of GraphQLClient to avoid the extraction process if it is not needed. This can be useful if you don't use operationName and want to optimise queries by reducing the amount of computation as much as possible, especially if we are in a context where we are using documents in string format to reduce bundle size.

// example where the operation name is not ignored
const client = new GraphQLClient(endpoint, {
  method: 'POST',
})
// example in which the operation name is ignored
const client = new GraphQLClient(endpoint, {
  method: 'POST',
  excludeOperationName: true,
})

Knowledge Base

Why was the file upload feature taken away? Will it return?

In this issue we decided to make this library more stable and maintainable. In principal the feature is still in scope of this library and will make a return when we find time to do the feature right.

Why do I have to install graphql?

graphql-request uses methods exposed by the graphql package to handle some internal logic. On top of that, for TypeScript users, some types are used from the graphql package to provide better typings.

Do I need to wrap my GraphQL documents inside the gql template exported by graphql-request?

No. It is there for convenience so that you can get the tooling support like automatic formatting and syntax highlighting. You can use gql from graphql-tag if you need it for some reason too.

What sets graphql-request apart from other clients like Apollo, Relay, etc.?

graphql-request is the most minimal and simplest to use GraphQL client. It's perfect for small scripts or simple apps.

Compared to GraphQL clients like Apollo or Relay, graphql-request doesn't have a built-in cache and has no integrations for frontend frameworks. The goal is to keep the package and API as minimal as possible.

Project Stats

Package Installs

NPM Usage Trend of graphql-request

Repo Beats

Alt