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 provide tools and utilities for interacting with GraphQL APIs from a client-side application. They handle tasks such as querying data, caching responses, and managing state related to GraphQL operations. Choosing the right library depends on factors like project requirements, performance needs, and developer preferences.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
graphql-request3,842,6125,738862 kB148 days agoMIT
apollo-client425,06619,270-5224 years agoMIT
urql219,6608,536324 kB272 months agoMIT
Feature Comparison: graphql-request vs apollo-client vs urql

Caching

  • graphql-request: graphql-request does not include built-in caching functionality, which means that you would need to implement your own caching strategy if required.
  • apollo-client: Apollo Client provides a sophisticated caching mechanism that allows you to store and retrieve GraphQL query results locally. This helps in reducing network requests and improving the overall performance of your application.
  • urql: Urql offers a flexible caching system that allows you to customize caching behavior based on your application's needs. It provides options for cache invalidation and data normalization.

Error Handling

  • graphql-request: graphql-request offers basic error handling features, such as catching network errors and GraphQL query errors.
  • apollo-client: Apollo Client provides robust error handling capabilities, including the ability to catch and handle errors at various stages of the GraphQL operation lifecycle.
  • urql: Urql includes error handling mechanisms that allow you to intercept and handle errors that occur during GraphQL operations. It provides hooks for error management and recovery.

Performance Optimization

  • graphql-request: graphql-request is designed for simplicity and efficiency, focusing on making lightweight and fast GraphQL queries without additional performance optimizations.
  • apollo-client: Apollo Client offers features like query deduplication, request batching, and intelligent caching strategies to optimize performance and reduce unnecessary network requests.
  • urql: Urql is optimized for performance, with a focus on minimizing bundle size and optimizing network requests. It provides tools for efficient data fetching and caching to improve overall application performance.

Integration with Frameworks

  • graphql-request: graphql-request is framework-agnostic and can be used with any frontend framework or library that supports HTTP requests.
  • apollo-client: Apollo Client has strong integration with popular frontend frameworks like React, Angular, and Vue, providing specific tools and utilities for seamless integration with these frameworks.
  • urql: Urql is designed to work well with React and provides hooks and utilities for integrating Urql with React components. It offers a React-specific API for managing GraphQL operations.

Developer Experience

  • graphql-request: graphql-request is straightforward to use and has a minimal API surface, making it easy to get started with making GraphQL queries in your application.
  • apollo-client: Apollo Client offers a rich developer experience with tools like Apollo DevTools, which provide insights into GraphQL queries, caching behavior, and network activity. It also has extensive documentation and community support.
  • urql: Urql aims to provide a developer-friendly experience with clear documentation, examples, and a focus on simplicity and efficiency in its API design.
How to Choose: graphql-request vs apollo-client vs urql
  • graphql-request: Choose graphql-request if you prefer a lightweight and minimalistic library for making simple queries to a GraphQL API. graphql-request is focused on providing a straightforward and efficient way to fetch data without the overhead of additional features.
  • apollo-client: Choose Apollo Client if you need a comprehensive solution with built-in caching, state management, and support for advanced features like subscriptions and file uploads. Apollo Client is well-suited for complex applications that require a high degree of flexibility and customization.
  • urql: Choose Urql if you value performance and efficiency, as it is designed to be highly optimized for speed and resource usage. Urql offers a small bundle size and efficient caching mechanisms, making it a good choice for applications where performance is a top priority.
Similar Npm Packages to graphql-request

graphql-request is a lightweight and flexible GraphQL client for Node.js and browsers. It allows developers to easily make GraphQL queries and mutations with minimal setup and configuration. While graphql-request provides a simple and efficient way to interact with GraphQL APIs, there are other libraries in the GraphQL ecosystem that offer similar functionality. Here are a few alternatives:

  • apollo-client is a comprehensive GraphQL client that provides features such as caching, local state management, and integration with Apollo's ecosystem of tools. It offers a robust solution for building GraphQL-powered applications.
  • urql is a lightweight and customizable GraphQL client that focuses on performance and flexibility. It offers features like automatic caching, SSR support, and a plugin system for extending functionality.

Check out this comparison: Comparing apollo-client vs graphql-request vs urql.

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