Which is Better GraphQL Data Fetching Libraries?
graphql-request

1 Year
graphql-requestSimilar Packages:
What's GraphQL Data Fetching Libraries?

GraphQL data fetching libraries provide a simple and efficient way to interact with GraphQL APIs. They abstract away the complexities of making HTTP requests and handling responses, allowing developers to focus on querying data and working with the results. These libraries typically offer features like query batching, caching, error handling, and query variables to streamline the process of fetching data from a GraphQL server.

NPM Package Downloads Trend
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
graphql-request3,883,4065,757862 kB13a month agoMIT
Feature Comparison: graphql-request

Query Batching

  • graphql-request: graphql-request supports query batching, allowing multiple queries to be sent in a single HTTP request. This can help reduce the number of network round trips and improve overall performance by fetching related data in a single request.

Caching

  • graphql-request: graphql-request does not provide built-in caching functionality. Developers may need to implement their own caching mechanism if caching is required for optimizing data fetching and reducing redundant requests.

Error Handling

  • graphql-request: graphql-request offers basic error handling capabilities, allowing developers to catch and handle errors returned by the GraphQL server. It provides a straightforward way to manage errors and display appropriate messages to users.

Query Variables

  • graphql-request: graphql-request supports query variables, enabling developers to pass dynamic values to GraphQL queries. This feature enhances reusability and flexibility by allowing parameters to be injected into queries at runtime.
How to Choose: graphql-request
  • graphql-request: Choose graphql-request if you need a lightweight and minimalistic library for making GraphQL queries. It is easy to set up and use, making it ideal for small to medium-sized projects where simplicity is a 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