Apollo-Server错误:countrycode.name在解析器中定义,但不存在于countrycode中

发布于 2025-01-18 16:14:35 字数 1612 浏览 1 评论 0原文

我最近升级了我的nodejs/express/graphQL项目依赖项,如下:

  • express:从4.17.1到4.17.3
  • apollo-server-express:从2.22.2到3.6.7
  • graphql:从15.5.0到16.3.0

我做了没有更改我的 typedefs/resolvers 中的任何内容,这些内容在更新之前工作正常,但现在我在实例化 ApolloServer 类时收到以下错误"apollo-server-express":

Error: CountryCode.name was defined in resolvers, but not present within CountryCode
at addResolversToSchema (/server/api/node_modules/@graphql-tools/schema/index.js:155:27)
at makeExecutableSchema (/server/api/node_modules/@graphql-tools/schema/index.js:501:14)
at ApolloServer.constructSchema (/server/api/node_modules/apollo-server-core/src/ApolloServer.ts:643:32)
at null.ApolloServerBase (/server/api/node_modules/apollo-server-core/src/ApolloServer.ts:283:18)
at null.ApolloServer (/server/api/node_modules/apollo-server-express/src/ApolloServer.ts:55:1)
at null.main (/server/api/builds/dev/api-server.js:191:22)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

注意错误消息:这不是常见的“xyz 在解析器中定义,但不在 typedef 中定义”类型的错误,这种错误在使用 graphQL 时经常发生,我知道如何修复. 有趣的是,CountryCode 是一个枚举,因此没有名为“name”的字段,如错误消息所示。以下是 CountryCode 枚举的 typedef:

const countryCodeTypedef = gql`
  enum CountryCode { af ax al <other-country-codes> }
`

其中 是所有剩余国家/地区代码的占位符。是的,我使用小写枚举值,尽管这不是最佳实践,但项目就是这样开始的,我现在无法更改它。不过在更新之前它运行得很好。

我查了一下,在我的解析器中没有 .name 字段的踪迹,在我的整个代码库中也没有!

如果我排除 CountryCode 枚举,服务器启动正常,但我的项目无法按预期工作。

v2 和 v3 之间 apollo-server 中定义 typedef/resolvers 的方式是否发生了变化?

有什么想法吗?

I recently upgraded my nodejs/express/graphQL project dependencies as follows:

  • express: from 4.17.1 to 4.17.3
  • apollo-server-express: from 2.22.2 to 3.6.7
  • graphql: from 15.5.0 to 16.3.0

I did not change anything in my typedefs/resolvers which worked fine before the update, but now I'm getting the following error upon instantiating the ApolloServer class from "apollo-server-express":

Error: CountryCode.name was defined in resolvers, but not present within CountryCode
at addResolversToSchema (/server/api/node_modules/@graphql-tools/schema/index.js:155:27)
at makeExecutableSchema (/server/api/node_modules/@graphql-tools/schema/index.js:501:14)
at ApolloServer.constructSchema (/server/api/node_modules/apollo-server-core/src/ApolloServer.ts:643:32)
at null.ApolloServerBase (/server/api/node_modules/apollo-server-core/src/ApolloServer.ts:283:18)
at null.ApolloServer (/server/api/node_modules/apollo-server-express/src/ApolloServer.ts:55:1)
at null.main (/server/api/builds/dev/api-server.js:191:22)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

Note the error message: this is not the usual "xyz defined in resolvers but not in typedefs" kind of error, which happens all the time when working with graphQL and I know how to fix.
What's funny is that CountryCode is an enum, and therefore has no field named "name", as the error message suggests. Here is the typedef for the CountryCode enum:

const countryCodeTypedef = gql`
  enum CountryCode { af ax al <other-country-codes> }
`

where <all-other-country-codes> is a placeholder for all remaining country codes. Yes, I use lower-case enum values although it is not best practice, but the project started out this way and I can't change it now. It worked fine before the update however.

I looked it up and there is no trace of a .name field in my resolvers, nor in my whole code-base!

If I exclude the CountryCode enum, the server starts fine, but then my project doesn't work as expected.

Did something change in the way typedefs/resolvers are defined in apollo-server between v2 and v3?

Any idea?

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

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

发布评论

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

评论(1

爱格式化 2025-01-25 16:14:35

今天早上淋浴时,我突然意识到错误的根本原因是什么!

作为升级过程的一部分,我还将“graphql-scalars”包从 1.9.0 升级到 1.17.0,结果发现其中添加了“CountryCode”标量,它与我自己的 Enum 定义相冲突。

我所拥有的一切要做的就是显式导入我需要的 graphQL 标量(在我将它们全部包含之前),并保留冲突的 CountryCode 标量

来自 apollo-server 的更具体的错误消息,例如。 “类型“CountryCode”的重复定义”会帮助我更快地发现错误,但事情就是这样:apollo-server 和 graphql 只是很棒的工具!

Under the shower this morning, I suddenly realized what the root cause of the error was!

As part of the upgrade process, I also upgraded the "graphql-scalars" package from 1.9.0 to 1.17.0 and it turned out that a CountryCode" scalar was added there, and it conflicted with my own Enum definition.

All I had to do was to explicitly import only the graphQL scalars I need (before I was including them all), and leave the conflicting CountryCode scalar out.

A more specific error message from apollo-server like 'duplicate definition for type "CountryCode"' would have helped me to spot the error faster, but that's how things are: apollo-server and graphql are just great tools!

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