apollo-client vs graphql-request vs urql
GraphQL 客户端库
apollo-clientgraphql-requesturql类似的npm包:

GraphQL 客户端库

GraphQL 客户端库用于与 GraphQL API 进行交互,提供了一种高效的方式来请求和管理数据。这些库简化了数据获取的过程,并提供了多种功能来优化性能、处理缓存和管理状态。选择合适的 GraphQL 客户端库可以显著提高开发效率和应用性能。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
apollo-client473,31219,721-4196 年前MIT
graphql-request06,117420 kB524 个月前MIT
urql08,949135 kB377 个月前MIT

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

功能丰富性

  • apollo-client:

    Apollo Client 提供了全面的功能,包括强大的缓存机制、状态管理、错误处理和本地数据支持。它允许开发者轻松地管理 GraphQL 查询和变更,并提供了丰富的开发者工具。

  • graphql-request:

    graphql-request 是一个极简的库,专注于发送 GraphQL 请求。它没有内置的缓存或状态管理功能,适合需要快速请求数据的场景。

  • urql:

    urql 提供了灵活的功能,支持多种数据获取策略和中间件。它允许开发者根据需求定制数据获取和缓存策略,适合需要灵活性的应用。

学习曲线

  • apollo-client:

    Apollo Client 的学习曲线相对较陡,因为它提供了许多功能和配置选项。开发者需要理解其缓存机制和状态管理,以充分利用其强大功能。

  • graphql-request:

    graphql-request 的学习曲线非常平缓,它的 API 设计简单明了,开发者可以快速上手并进行基本的 GraphQL 请求。

  • urql:

    urql 的学习曲线适中,虽然它提供了许多功能,但文档清晰,开发者可以逐步掌握其用法。

性能

  • apollo-client:

    Apollo Client 的性能依赖于其缓存机制,合理配置缓存可以显著提高数据获取的效率。然而,复杂的查询和变更可能导致性能问题,需要开发者进行优化。

  • graphql-request:

    graphql-request 因为其轻量级的特性,性能表现良好,适合快速请求数据,但缺乏缓存机制可能导致重复请求。

  • urql:

    urql 提供了良好的性能,支持灵活的缓存策略和数据获取方式,开发者可以根据需要优化性能。

社区支持

  • apollo-client:

    Apollo Client 拥有一个活跃的社区和丰富的文档资源,开发者可以轻松找到支持和解决方案。

  • graphql-request:

    graphql-request 的社区相对较小,但由于其简单性,开发者可以快速找到基本的使用示例。

  • urql:

    urql 也有一个活跃的社区,提供了良好的文档和示例,开发者可以获得支持和灵感。

扩展性

  • apollo-client:

    Apollo Client 提供了丰富的扩展性,支持多种插件和中间件,开发者可以根据需求定制功能。

  • graphql-request:

    graphql-request 的扩展性有限,主要专注于发送请求,不支持复杂的功能扩展。

  • urql:

    urql 设计为可扩展,允许开发者根据具体需求添加自定义的中间件和功能,适合需要灵活性的应用。

如何选择: apollo-client vs graphql-request vs urql

  • apollo-client:

    选择 Apollo Client 如果你需要一个全面的解决方案,支持复杂的状态管理、缓存和本地数据。它适合大型应用程序,并且有丰富的生态系统和社区支持。

  • graphql-request:

    选择 graphql-request 如果你需要一个轻量级的库来快速发送 GraphQL 请求,且不需要复杂的状态管理或缓存功能。它简单易用,适合小型项目或快速原型开发。

  • urql:

    选择 urql 如果你需要一个灵活且可扩展的库,支持多种数据获取策略和中间件。urql 提供了良好的性能和可定制性,适合中型到大型应用程序。

apollo-client的README

Apollo Client npm version Open Source Helpers Join the community on Spectrum

Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to easily build UI components that fetch data via GraphQL. To get the most value out of apollo-client, you should use it with one of its view layer integrations.

To get started with the React integration, go to our React Apollo documentation website.

Apollo Client also has view layer integrations for all the popular frontend frameworks. For the best experience, make sure to use the view integration layer for your frontend framework of choice.

Apollo Client can be used in any JavaScript frontend where you want to use data from a GraphQL server. It's:

  1. Incrementally adoptable, so that you can drop it into an existing JavaScript app and start using GraphQL for just part of your UI.
  2. Universally compatible, so that Apollo works with any build setup, any GraphQL server, and any GraphQL schema.
  3. Simple to get started with, so you can start loading data right away and learn about advanced features later.
  4. Inspectable and understandable, so that you can have great developer tools to understand exactly what is happening in your app.
  5. Built for interactive apps, so your users can make changes and see them reflected in the UI immediately.
  6. Small and flexible, so you don't get stuff you don't need. The core is under 25kb compressed.
  7. Community driven, because Apollo is driven by the community and serves a variety of use cases. Everything is planned and developed in the open.

Get started on the home page, which has great examples for a variety of frameworks.

Installation

# installing the preset package
npm install apollo-boost graphql-tag graphql --save
# installing each piece independently
npm install apollo-client apollo-cache-inmemory apollo-link-http graphql-tag graphql --save

To use this client in a web browser or mobile app, you'll need a build system capable of loading NPM packages on the client. Some common choices include Browserify, Webpack, and Meteor 1.3+.

Install the Apollo Client Developer tools for Chrome for a great GraphQL developer experience!

Usage

You get started by constructing an instance of the core class ApolloClient. If you load ApolloClient from the apollo-boost package, it will be configured with a few reasonable defaults such as our standard in-memory cache and a link to a GraphQL API at /graphql.

import ApolloClient from 'apollo-boost';

const client = new ApolloClient();

To point ApolloClient at a different URL, add your GraphQL API's URL to the uri config property:

import ApolloClient from 'apollo-boost';

const client = new ApolloClient({
  uri: 'https://graphql.example.com'
});

Most of the time you'll hook up your client to a frontend integration. But if you'd like to directly execute a query with your client, you may now call the client.query method like this:

import gql from 'graphql-tag';

client.query({
  query: gql`
    query TodoApp {
      todos {
        id
        text
        completed
      }
    }
  `,
})
  .then(data => console.log(data))
  .catch(error => console.error(error));

Now your client will be primed with some data in its cache. You can continue to make queries, or you can get your client instance to perform all sorts of advanced tasks on your GraphQL data. Such as reactively watching queries with watchQuery, changing data on your server with mutate, or reading a fragment from your local cache with readFragment.

To learn more about all of the features available to you through the apollo-client package, be sure to read through the apollo-client API reference.

Learn how to use Apollo Client with your favorite framework


Contributing

CircleCI codecov

Read the Apollo Contributor Guidelines.

Running tests locally:

npm install
npm test

This project uses TypeScript for static typing and TSLint for linting. You can get both of these built into your editor with no configuration by opening this project in Visual Studio Code, an open source IDE which is available for free on all platforms.

Important discussions

If you're getting booted up as a contributor, here are some discussions you should take a look at:

  1. Static typing and why we went with TypeScript also covered in the Medium post
  2. Idea for pagination handling
  3. Discussion about interaction with Redux and domain vs. client state
  4. Long conversation about different client options, before this repo existed

Maintainers