'apollo-server Error: CountryCode.name was defined in resolvers, but not present within CountryCode
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?
Solution 1:[1]
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!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | salai |
