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

GraphQL client libraries are essential tools for managing data interactions between a client application and a GraphQL server. They provide abstractions for querying, mutating, and caching data, enabling developers to efficiently handle complex data-fetching scenarios. These libraries streamline the process of integrating GraphQL into applications, offering features like automatic caching, real-time updates, and optimized request handling, which enhance the overall performance and user experience of web applications.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
graphql-request4,164,5915,846862 kB244 months agoMIT
apollo-client427,75119,363-5164 years agoMIT
urql230,5328,640325 kB274 days agoMIT
Feature Comparison: graphql-request vs apollo-client vs urql

Caching Mechanism

  • graphql-request: GraphQL Request does not include built-in caching features, making it a straightforward option for simple use cases. Developers must implement their own caching strategies if needed, which can be beneficial for small applications where complexity is not required.
  • apollo-client: Apollo Client offers a sophisticated caching mechanism that allows for normalized data storage, enabling efficient updates and retrieval of data. It automatically caches responses and can intelligently manage cache updates based on mutations, significantly improving performance and reducing network requests.
  • urql: Urql provides a flexible caching strategy that can be customized based on the needs of the application. It supports both local and global caching, allowing developers to choose how to manage data persistence and retrieval effectively.

Ease of Use

  • graphql-request: GraphQL Request is extremely easy to use, with a minimal API that allows developers to make requests with just a few lines of code. This makes it an excellent choice for beginners or for quick prototypes where simplicity is key.
  • apollo-client: Apollo Client is relatively easy to use, especially for those familiar with React, as it provides a set of hooks and components that simplify data fetching and state management. However, its extensive feature set may introduce some complexity for new users.
  • urql: Urql strikes a balance between ease of use and flexibility. It offers a straightforward API while allowing for customization through its plugin system, making it approachable for developers of varying skill levels.

Real-time Capabilities

  • graphql-request: GraphQL Request does not natively support real-time capabilities, as it focuses on making simple HTTP requests. Developers would need to implement additional solutions, such as WebSockets, for real-time functionality.
  • apollo-client: Apollo Client supports real-time capabilities through subscriptions, allowing applications to receive live updates from the server. This feature is essential for applications that require real-time data synchronization, such as chat applications or collaborative tools.
  • urql: Urql has built-in support for subscriptions, enabling real-time data updates. It provides a straightforward way to integrate real-time capabilities into applications, making it suitable for scenarios where live data is essential.

Extensibility

  • graphql-request: GraphQL Request is not designed for extensibility, as it focuses on providing a simple interface for making requests. This can be a limitation for applications that require more advanced features or integrations.
  • apollo-client: Apollo Client is highly extensible, allowing developers to integrate various middleware and plugins to enhance its functionality. This makes it suitable for complex applications that require custom behavior or integrations with other libraries.
  • urql: Urql is designed with extensibility in mind, offering a plugin system that allows developers to add custom functionalities easily. This makes it adaptable to different project requirements and use cases.

Community and Ecosystem

  • graphql-request: GraphQL Request has a smaller community compared to Apollo Client, but it is still well-documented and supported. Its simplicity attracts a niche audience looking for lightweight solutions.
  • apollo-client: Apollo Client has a large and active community, along with a rich ecosystem of tools and libraries, including Apollo Server and Apollo Federation. This extensive support makes it easier to find resources, tutorials, and community help.
  • urql: Urql has a growing community and is part of the larger GraphQL ecosystem. While not as extensive as Apollo's, it benefits from active development and contributions, making it a viable option for many developers.
How to Choose: graphql-request vs apollo-client vs urql
  • graphql-request: Choose GraphQL Request for its simplicity and minimalism. It is perfect for smaller projects or when you need a lightweight solution without the overhead of a full-featured client. It allows you to make GraphQL requests easily without additional features that may not be necessary for your use case.
  • apollo-client: Choose Apollo Client if you need a comprehensive solution that includes advanced features like caching, state management, and support for subscriptions. It is ideal for larger applications where managing complex data interactions is crucial, and it integrates seamlessly with React and other frameworks.
  • urql: Choose Urql if you want a flexible and customizable GraphQL client that balances simplicity and extensibility. It is suitable for projects that require a modular approach, allowing you to pick and choose features as needed, making it a good fit for both small and medium-sized applications.
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