apollo-client vs graphql-tag vs react-apollo
GraphQL 客户端库
apollo-clientgraphql-tagreact-apollo类似的npm包:

GraphQL 客户端库

GraphQL 客户端库用于简化与 GraphQL API 的交互,提供了高效的数据获取和管理功能。这些库帮助开发者构建更灵活的前端应用,能够根据需要请求特定的数据,减少了数据传输的冗余。通过使用这些库,开发者可以更好地管理应用的状态,并简化数据的获取和更新过程。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
apollo-client019,720-4196 年前MIT
graphql-tag02,333-1004 年前MIT
react-apollo06,805-2056 年前MIT

功能对比: apollo-client vs graphql-tag vs react-apollo

数据获取

  • apollo-client:

    Apollo Client 提供了强大的数据获取功能,支持查询、变更和订阅。它能够自动处理缓存,确保应用始终使用最新的数据,同时减少不必要的网络请求。

  • graphql-tag:

    graphql-tag 主要用于定义 GraphQL 查询,它将查询字符串转换为可执行的 GraphQL 查询对象。虽然它本身不处理数据获取,但与其他库结合使用时,可以简化查询的定义和管理。

  • react-apollo:

    React Apollo 使得在 React 组件中获取数据变得简单。通过使用高阶组件或钩子,开发者可以轻松地将 GraphQL 查询与组件的生命周期结合起来,自动处理数据的加载和错误状态。

状态管理

  • apollo-client:

    Apollo Client 内置了状态管理功能,可以将本地状态与远程 GraphQL 数据结合起来。开发者可以使用 Apollo 的缓存机制来管理应用的状态,避免了额外的状态管理库。

  • graphql-tag:

    graphql-tag 不涉及状态管理,它的主要功能是解析 GraphQL 查询。状态管理通常需要与其他库结合使用。

  • react-apollo:

    React Apollo 通过 Apollo Client 提供的状态管理功能,允许开发者在 React 组件中直接使用 GraphQL 数据。它使得状态管理与数据获取紧密结合,简化了开发流程。

学习曲线

  • apollo-client:

    Apollo Client 的学习曲线相对较陡,因为它提供了许多功能和配置选项。开发者需要理解缓存机制、查询和变更的使用,以及如何与 GraphQL API 进行交互。

  • graphql-tag:

    graphql-tag 的学习曲线非常平缓,因为它的功能简单明了。开发者只需了解如何定义和使用 GraphQL 查询即可。

  • react-apollo:

    React Apollo 的学习曲线也相对较低,特别是对于已经熟悉 React 的开发者。它提供了直观的 API,能够快速上手并集成到现有的 React 应用中。

扩展性

  • apollo-client:

    Apollo Client 具有很高的扩展性,支持插件和自定义链接,允许开发者根据需求扩展其功能。它可以与其他库和工具无缝集成,适应不同的应用场景。

  • graphql-tag:

    graphql-tag 的扩展性有限,因为它的功能专注于查询字符串的解析。它通常与其他库结合使用来实现更复杂的功能。

  • react-apollo:

    React Apollo 的扩展性体现在其与 Apollo Client 的深度集成上。开发者可以通过自定义的高阶组件和钩子来扩展功能,满足特定的需求。

性能优化

  • apollo-client:

    Apollo Client 提供了多种性能优化机制,如查询去重、缓存策略和懒加载等。开发者可以根据应用的需求选择合适的优化策略,以提高性能。

  • graphql-tag:

    graphql-tag 本身不涉及性能优化,但通过合理的查询设计和与其他库的结合使用,可以帮助提高数据获取的效率。

  • react-apollo:

    React Apollo 通过自动管理数据的加载和更新,减少了不必要的渲染,从而提高了性能。开发者可以使用 React 的性能优化技术,如 memoization 和 shouldComponentUpdate,进一步提升性能。

如何选择: apollo-client vs graphql-tag vs react-apollo

  • apollo-client:

    选择 Apollo Client 如果你需要一个全面的解决方案,支持缓存、状态管理和与 GraphQL 的深度集成。它适合大型应用,能够处理复杂的查询和变更,并提供强大的开发者工具。

  • graphql-tag:

    选择 graphql-tag 如果你需要一个轻量级的工具来解析 GraphQL 查询字符串。它非常适合与其他库结合使用,尤其是在需要动态构建查询的场景中。

  • react-apollo:

    选择 React Apollo 如果你正在使用 React,并希望将 Apollo Client 与 React 组件无缝集成。它提供了方便的高阶组件和钩子,使得在 React 应用中使用 GraphQL 变得简单。

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