graphql-request vs urql vs apollo-client vs react-apollo
GraphQL 客户端库
graphql-requesturqlapollo-clientreact-apollo类似的npm包:

GraphQL 客户端库

GraphQL 客户端库用于简化与 GraphQL API 的交互,它们提供了用于发送查询和变更请求的工具,并管理响应数据的状态。这些库各具特色,适用于不同的开发需求和项目规模。选择合适的库可以提升开发效率,简化数据管理,改善应用性能。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
graphql-request7,693,7616,116420 kB493 个月前MIT
urql679,3408,939135 kB356 个月前MIT
apollo-client019,724-4236 年前MIT
react-apollo06,810-2056 年前MIT

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

功能性

  • graphql-request:

    graphql-request 是一个简单的 HTTP 客户端,专注于发送 GraphQL 请求。它的 API 简洁易用,适合快速开发和小型项目,不提供缓存或状态管理功能。

  • urql:

    urql 是一个灵活的 GraphQL 客户端,支持多种数据获取策略和插件系统。它允许开发者根据需求自定义缓存和请求逻辑,适合需要高度可定制的应用。

  • apollo-client:

    Apollo Client 是一个功能强大的 GraphQL 客户端,提供了缓存、状态管理和开发者工具,适合复杂的应用程序。它支持多种数据获取方式,包括查询、变更和订阅,并提供了丰富的 API 来处理数据。

  • react-apollo:

    React Apollo 是 Apollo Client 的 React 绑定,提供了方便的组件和钩子,使得在 React 应用中使用 GraphQL 数据变得简单。它支持自动缓存和更新 UI,增强了开发体验。

学习曲线

  • graphql-request:

    graphql-request 的学习曲线非常平缓,几乎不需要学习成本,适合快速上手和简单的 API 调用。

  • urql:

    urql 的学习曲线适中,提供了灵活的 API 和文档,适合有一定经验的开发者进行自定义配置。

  • apollo-client:

    Apollo Client 的学习曲线相对较陡,尤其是对于初学者。它的功能丰富,涉及缓存和状态管理的概念,需要一定的时间来掌握。

  • react-apollo:

    React Apollo 的学习曲线与 React 相似,开发者需要了解 React 的基本概念,但一旦掌握,使用 GraphQL 数据会变得非常直观。

性能

  • graphql-request:

    graphql-request 的性能非常高效,因为它是一个轻量级的库,直接发送请求,不涉及复杂的缓存机制,适合快速的 API 调用。

  • urql:

    urql 允许开发者根据需求自定义缓存策略,提供了灵活的性能优化选项,适合需要高性能的应用。

  • apollo-client:

    Apollo Client 提供了强大的缓存机制,可以显著提高应用的性能,减少网络请求。但如果不合理使用,可能会导致性能下降,特别是在处理大量数据时。

  • react-apollo:

    React Apollo 通过自动缓存和更新 UI 来提高性能,能够有效减少不必要的网络请求,提升用户体验。

扩展性

  • graphql-request:

    graphql-request 的扩展性有限,主要用于发送请求,不支持复杂的扩展和自定义功能。

  • urql:

    urql 的设计理念是高度可扩展,支持插件系统,允许开发者根据需求添加自定义功能,适合需要灵活性的项目。

  • apollo-client:

    Apollo Client 的扩展性很强,支持多种中间件和插件,可以根据项目需求进行定制,适合大型应用。

  • react-apollo:

    React Apollo 的扩展性依赖于 Apollo Client,支持与其他 Apollo 生态系统工具集成,适合需要与 GraphQL 生态系统紧密结合的应用。

社区支持

  • graphql-request:

    graphql-request 的社区相对较小,但由于其简单性,文档清晰,容易找到解决方案。

  • urql:

    urql 也有一个活跃的社区,提供了良好的文档和支持,适合需要社区互动的开发者。

  • apollo-client:

    Apollo Client 拥有一个活跃的社区和丰富的文档,提供了大量的教程和支持,适合需要社区资源的开发者。

  • react-apollo:

    React Apollo 作为 Apollo Client 的一部分,享有良好的社区支持和丰富的资源,适合需要社区帮助的开发者。

如何选择: 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 组件无缝集成,提供更强大的数据管理和 UI 绑定功能。

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