graphql-modules
graphql-modules copied to clipboard
[v1][regression][RFC] Cannot use typescript enums to resolve graphql enum types internal values anymore
Hi :) My backend forces different values for an enum internally than in the public API, so I'm mapping those values in the resolvers: https://www.apollographql.com/docs/apollo-server/schema/schema/#internal-values-advanced
With Apollo this usually translates to:
import {Resolvers} from '../../../types/graphql';
const resolvers: Resolvers = {
Sex: {
MALE: 0,
FEMALE: 1,
},
With graphql-modules v0 I used to use the following shortcut:
import {Resolvers, Sex} from '../../../types/graphql';
const resolvers: Resolvers = {
Sex,
where Sex is an enum autogenerated by the codegen:
export enum Sex {
MALE = 0,
FEMALE = 1
}
With graphql-modules v1 enums cannot be used anymore as resolvers:
(node:46126) UnhandledPromiseRejectionWarning: Error: Sex.0 was defined in resolvers, but not present within Sex
at /home/niko/devel/beach/server/dist/schema/src/addResolversToSchema.js:55:31
at Array.forEach (<anonymous>)
at /home/niko/devel/beach/server/dist/schema/src/addResolversToSchema.js:50:44
at Array.forEach (<anonymous>)
at addResolversToSchema (/home/niko/devel/beach/server/dist/schema/src/addResolversToSchema.js:18:28)
at schemaTransforms (/home/niko/devel/beach/server/dist/schema/src/makeExecutableSchema.js:66:41)
at /home/niko/devel/beach/server/dist/schema/src/makeExecutableSchema.js:108:65
at Array.reduce (<anonymous>)
at makeExecutableSchema (/home/niko/devel/beach/server/dist/schema/src/makeExecutableSchema.js:108:29)
at applicationFactory (/home/niko/devel/beach/server/dist/graphql-modules/src/application/application.js:75:80)
at Object.createApplication (/home/niko/devel/beach/server/dist/graphql-modules/src/application/application.js:117:12)
at Object.createApp (/home/niko/devel/beach/server/src/modules/index.ts:11:3)
at /home/niko/devel/beach/server/src/index.ts:29:18
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:46126) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:46126) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
This is the same behaviour as plain Apollo without graphql-modules v0, so it wouldn't be a big deal except for the fact that I want to avoid duplicate code and keep the schema as the only source of truth (see also https://github.com/dotansimha/graphql-code-generator/issues/3623#issuecomment-599473810).
Since this trick didn't work without graphql-modules in the first place I'm not sure if we should restore the old v0 behaviour. On the other hand manually writing resolvers for enums internal values is stupid, so maybe this could be better suited as automatically generated resolvers in the codegen?