apollo客户端,gql来自graphql-tag或 @apollo/client
我有一些疑问
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个导入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.从'@apollo/client'和
导入的
导入{gql}之间的差异来自'graphql-tag'
import gql现代阿波罗客户端项目。此导入语句来自Apollo Client 3,后者将GQL标签集成在其自己的软件包中。当您与Apollo Client 3或更高版本合作时,建议使用此方法,因为它将所有必要的功能整合到一个软件包中,从而简化了设置和依赖关系管理。
此导入语句来自GraphQL-TAG库,该库通常与Apollo Client的早期版本(Apollo Client 3)结合使用。 GraphQL-TAG库是一个单独的软件包,可让您将GraphQl查询字符串解析为Apollo Client所需的格式。
结论
从'@apollo/client';
对于现代阿波罗客户端项目( 阿波罗客户端的最新功能和改进。
如果您正在使用旧的代码库或Apollo客户端的版本,则可能仍然会遇到:
从'graphql-tag';
导入gql
The difference between
import { gql } from '@apollo/client'
andimport gql from 'graphql-tag'
lies in the libraries they originate from and how they are used in modern Apollo Client projects.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.
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
import { gql } from '@apollo/client';
:This keeps your dependencies streamlined and leverages the latest features and improvements from Apollo Client.
If you're working with older codebases or versions of Apollo Client, you might still encounter:
import gql from 'graphql-tag';