apollo客户端,gql来自graphql-tag或 @apollo/client

发布于 2025-01-23 11:33:20 字数 284 浏览 2 评论 0原文

我有一些疑问

import { gql } from '@apollo/client'; // from Apollo Client 3

,还有其他问题:

import gql from 'graphql-tag';

使用一个或彼此之间有什么区别?

我不得不将导入的一个从阿波罗/客户端更改为GraphQl-tag,因为MockedProvider实用程序以从Apollo客户端进行测试的某些问题,但是,为什么?

I have some queries with

import { gql } from '@apollo/client'; // from Apollo Client 3

and other with:

import gql from 'graphql-tag';

What's the difference between using one or another?

I had to change the one imported from apollo/client to graphql-tag because some problems with MockedProvider utility for testing from Apollo Client, but, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2025-01-30 11:33:20

第一个导入Apollo客户端库Rexports和第二个直接导入帮助者的帮助者。
我会使用第一种方式,因为它可以确保您使用Apollo客户端期望的版本。
如果您将graphQl-tag作为直接依赖关系或其他依赖关系需要在其他版本中,则第二个可以为您提供不同的版本。

The first imports the helpers that the apollo client library rexports and the second imports the helpers directly.
I'd use the first way, as it ensures you use the version that apollo client expects.
The second one can give you a different version if you have graphql-tag as a direct dependency or another dependency requires it in a different version.

只是我以为 2025-01-30 11:33:20

从'@apollo/client'和导入的导入{gql}之间的差异来自'graphql-tag' import gql现代阿波罗客户端项目。

import { gql } from '@apollo/client'

此导入语句来自Apollo Client 3,后者将GQL标签集成在其自己的软件包中。当您与Apollo Client 3或更高版本合作时,建议使用此方法,因为它将所有必要的功能整合到一个软件包中,从而简化了设置和依赖关系管理。

import gql from 'graphql-tag'

此导入语句来自GraphQL-TAG库,该库通常与Apollo Client的早期版本(Apollo Client 3)结合使用。 GraphQL-TAG库是一个单独的软件包,可让您将GraphQl查询字符串解析为Apollo Client所需的格式。

结论

  1. 版本3及更高版本),优先使用:

从'@apollo/client';

对于现代阿波罗客户端项目( 阿波罗客户端的最新功能和改进。

  1. 如果您正在使用旧的代码库或Apollo客户端的版本,则可能仍然会遇到:

    从'graphql-tag';

    导入gql

The difference between import { gql } from '@apollo/client' and import gql from 'graphql-tag' lies in the libraries they originate from and how they are used in modern Apollo Client projects.

import { gql } from '@apollo/client'

This import statement is from Apollo Client 3, which integrates the gql tag within its own package. Using this method is recommended when you are working with Apollo Client 3 or later versions because it consolidates all necessary functionalities into one package, simplifying the setup and dependency management.

import gql from 'graphql-tag'

This import statement is from the graphql-tag library, which was commonly used in conjunction with earlier versions of Apollo Client (prior to Apollo Client 3). The graphql-tag library is a separate package that allows you to parse GraphQL query strings into the format needed by Apollo Client.

Conclusion

  1. for modern Apollo Client projects (version 3 and above), prefer using:

import { gql } from '@apollo/client';:

This keeps your dependencies streamlined and leverages the latest features and improvements from Apollo Client.

  1. If you're working with older codebases or versions of Apollo Client, you might still encounter:

    import gql from 'graphql-tag';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文