graphql-request vs urql vs apollo-client vs react-apollo
GraphQL クライアントライブラリ
graphql-requesturqlapollo-clientreact-apollo類似パッケージ:
GraphQL クライアントライブラリ

GraphQL クライアントライブラリは、GraphQL API と通信するためのツールを提供します。これらのライブラリは、データの取得、キャッシング、ミューテーションの実行、サブスクリプションの管理を簡素化し、開発者が効率的に GraphQL を利用できるようにします。これにより、クライアント側のデータ管理が容易になり、アプリケーションのパフォーマンスが向上します。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
graphql-request6,567,1716,087420 kB422日前MIT
urql495,0608,905135 kB354ヶ月前MIT
apollo-client454,70219,682-4256年前MIT
react-apollo113,0506,818-2056年前MIT
機能比較: graphql-request vs urql vs apollo-client vs react-apollo

キャッシング

  • graphql-request:

    graphql-request は、キャッシング機能を持たず、シンプルな API 呼び出しを行うためのライブラリです。データのキャッシングが不要な場合や、リクエストのオーバーヘッドを避けたい場合に適しています。

  • urql:

    urql は、軽量なキャッシング機能を提供し、必要に応じてカスタマイズ可能です。デフォルトのキャッシング戦略を使用するか、独自のキャッシングロジックを実装することができます。

  • apollo-client:

    Apollo Client は、強力なキャッシング機能を提供し、データの再取得を最小限に抑えます。クエリの結果をキャッシュし、同じデータを再リクエストする際にキャッシュを利用することで、パフォーマンスを向上させます。

  • react-apollo:

    React Apollo は、Apollo Client のキャッシング機能を活用し、React コンポーネントにデータを効率的に提供します。クエリの結果をコンポーネントの状態として管理し、再レンダリングを最適化します。

使用シナリオ

  • graphql-request:

    graphql-request は、シンプルな API 呼び出しを行う小規模なプロジェクトや、テストのためのクライアントとして使用するのに適しています。

  • urql:

    urql は、軽量で柔軟な GraphQL クライアントとして、小規模から中規模のアプリケーションに適しています。カスタマイズ性が高く、特定のニーズに合わせて拡張可能です。

  • apollo-client:

    Apollo Client は、複雑なデータ依存関係を持つ大規模なアプリケーションに最適です。特に、リアルタイムデータやサブスクリプションを使用する場合に強力です。

  • react-apollo:

    React Apollo は、React アプリケーションにおいて、データを簡単に取得し、コンポーネントにバインドするために最適です。特に、React のライフサイクルと統合されているため、使いやすいです。

学習曲線

  • graphql-request:

    graphql-request は、シンプルな API 呼び出しを提供するため、学習曲線は非常に緩やかです。すぐに使い始めることができます。

  • urql:

    urql は、シンプルで直感的な API を提供しており、学習曲線は緩やかです。特に、React と組み合わせて使用する場合は、すぐに習得できます。

  • apollo-client:

    Apollo Client は、多機能であるため、学習曲線がやや急です。しかし、豊富なドキュメントとコミュニティサポートがあるため、学習をサポートします。

  • react-apollo:

    React Apollo は、React の知識があれば比較的簡単に学習できます。React のコンポーネントと統合されているため、自然な形で学ぶことができます。

拡張性

  • graphql-request:

    graphql-request は、シンプルな設計のため、拡張性は限られていますが、必要に応じて他のライブラリと組み合わせて使用することができます。

  • urql:

    urql は、プラグインシステムを持ち、カスタムエクスチェンジを追加することで、機能を拡張できます。特に、特定のニーズに合わせたカスタマイズが可能です。

  • apollo-client:

    Apollo Client は、プラグインシステムを持ち、さまざまな機能を拡張することができます。カスタムリンクやキャッシング戦略を追加することで、ニーズに合わせた柔軟な構成が可能です。

  • react-apollo:

    React Apollo は、Apollo Client の拡張性を活かし、React コンポーネントに特化した機能を提供します。カスタムフックを作成することで、さらに拡張することができます。

パフォーマンス

  • graphql-request:

    graphql-request は、軽量で迅速なリクエストを提供しますが、キャッシング機能がないため、毎回リクエストを行う必要があります。

  • urql:

    urql は、軽量で迅速なパフォーマンスを提供し、必要に応じてカスタマイズ可能です。特に、データの取得と更新の効率が高いです。

  • apollo-client:

    Apollo Client は、デフォルトで最適化されたパフォーマンスを提供しますが、複雑なクエリや大規模なデータセットでは、パフォーマンスの調整が必要になる場合があります。

  • react-apollo:

    React Apollo は、React のライフサイクルに基づいてデータを管理するため、パフォーマンスが最適化されます。再レンダリングを最小限に抑えることができます。

選び方: graphql-request vs urql vs apollo-client vs react-apollo
  • graphql-request:

    graphql-request は、シンプルで軽量な GraphQL クライアントを求める場合に適しています。小規模なプロジェクトや、簡単な API 呼び出しを行う場合に最適です。

  • urql:

    urql は、軽量で柔軟な GraphQL クライアントを提供し、特にカスタマイズ性を重視する場合に適しています。小規模から中規模のアプリケーションに最適で、簡単に拡張可能です。

  • apollo-client:

    Apollo Client は、豊富な機能と強力なキャッシング機能を必要とする大規模なアプリケーションに最適です。特に、複雑なデータ依存関係を持つ場合や、サーバーとのインタラクションが多い場合に選択してください。

  • react-apollo:

    React Apollo は、React アプリケーションに特化した Apollo Client のラッパーです。React のコンポーネントライフサイクルと統合されているため、React を使用している場合は、これを選択するのが良いでしょう。

graphql-request のREADME

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