graphql-request vs urql vs apollo-client vs react-apollo
GraphQL 客戶端庫
graphql-requesturqlapollo-clientreact-apollo類似套件:

GraphQL 客戶端庫

GraphQL 客戶端庫提供了一種與 GraphQL 伺服器進行交互的方式,幫助開發者發送查詢和變更請求,並處理響應數據。這些庫通常包括狀態管理、緩存和錯誤處理等功能,旨在簡化與 GraphQL API 的交互過程。選擇合適的庫可以提高開發效率,改善應用性能,並提供更好的用戶體驗。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
graphql-request13,112,8486,121420 kB601 年前MIT
urql1,518,1508,985140 kB332 個月前MIT
apollo-client583,95619,781-4106 年前MIT
react-apollo167,0586,790-2057 年前MIT

功能比較: graphql-request vs urql vs apollo-client vs react-apollo

功能強度

  • graphql-request:

    graphql-request 是一個極簡的庫,專注於發送 GraphQL 請求,沒有內建的緩存或狀態管理功能,適合需要快速簡單請求的場景。

  • urql:

    urql 是一個靈活的 GraphQL 客戶端,提供可擴展的架構,支持自定義請求和緩存邏輯,適合需要高度自定義的應用。

  • apollo-client:

    Apollo Client 是一個功能全面的 GraphQL 客戶端,提供緩存、狀態管理、錯誤處理和開發者工具。它支持多種 GraphQL 特性,如訂閱和本地狀態管理,適合大型應用。

  • react-apollo:

    React Apollo 是基於 Apollo Client 的一個庫,專為 React 應用設計,提供了與 React 組件的緊密集成,支持查詢、變更和訂閱。

學習曲線

  • graphql-request:

    graphql-request 的學習曲線非常平緩,因為它的 API 簡單易懂,適合初學者和快速開發。

  • urql:

    urql 的學習曲線相對較低,因為它的 API 簡單且直觀,適合快速上手和開發。

  • 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 利用 Apollo Client 的緩存機制,能夠有效管理數據並提高性能,特別是在大型應用中。

擴展性

  • 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 受益於 Apollo Client 的社區支持,擁有豐富的資源和範例,適合 React 開發者。

如何選擇: graphql-request vs urql vs apollo-client vs react-apollo

  • graphql-request:

    選擇 graphql-request 如果你需要一個輕量級的庫,專注於簡單的 GraphQL 請求,並且不需要複雜的狀態管理或緩存功能。它非常適合小型應用或快速原型開發。

  • urql:

    選擇 urql 如果你需要一個靈活且可擴展的 GraphQL 客戶端,並且希望能夠輕鬆地自定義請求和緩存邏輯。urql 提供了簡單的 API 和可插拔的架構,適合中小型應用。

  • apollo-client:

    選擇 Apollo Client 如果你需要一個功能強大的解決方案,支持緩存、狀態管理和多種 GraphQL 特性,並且希望與 React 生態系統緊密集成。它適合大型應用程序,並提供了豐富的開發者工具。

  • react-apollo:

    選擇 React Apollo 如果你的應用是基於 React 的,並且你希望利用 Apollo 的強大功能來處理 GraphQL 數據,特別是當你需要與 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