Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1][regression][RFC] Cannot use typescript enums to resolve graphql enum types internal values anymore #1692

Open
darkbasic opened this issue May 18, 2021 · 0 comments

Comments

@darkbasic
Copy link
Contributor

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 dotansimha/graphql-code-generator#3623 (comment)).

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant