graphql-code-generator: `Maybe` is inlined to include `undefined` in `typescript-operations` version 2.1.5
Describe the bug
When using the preResolveTypes option, Maybe types now include undefined in their union starting in version 2.1.5, regardless of any avoidOptionals options.
To Reproduce Steps to reproduce the behavior: https://codesandbox.io/s/amazing-montalcini-kuf9p?file=/codegen.yml Code Sandbox isn’t inlining Maybes for some reason right now. Maybe the dependencies aren’t updating correctly.
- My GraphQL schema:
type Query {
user(id: ID!): User!
}
type User {
id: ID!
username: String!
email: String
}
- My GraphQL operations:
# Put your operations here
query user {
user(id: 1) {
id
username
email
}
}
- My
codegen.ymlconfig file:
schema: schema.graphql
documents: document.graphql
generates:
types.ts:
plugins:
- typescript
- typescript-operations
config:
avoidOptionals: true
preResolveTypes: true
Expected behavior
The codesandbox isn’t inlining maybes for some reason.
An example change for an operation. Maybe is being inlined as T | null | undefined even though avoidOptionals is true.
-export type CypressCreateOrgMutation = { newAccount: Maybe<{ __typename: 'Account', id: string }> };
+export type CypressCreateOrgMutation = { newAccount: { __typename: 'Account', id: string } | null | undefined };
Environment:
"@graphql-codegen/add": "^2.0.2",
"@graphql-codegen/cli": "2.2.0",
"@graphql-codegen/plugin-helpers": "^2.1.1",
"@graphql-codegen/typescript": "^2.2.2",
"@graphql-codegen/typescript-operations": "^2.1.5",
"@graphql-codegen/typescript-resolvers": "^1.19.1",
"@graphql-codegen/visitor-plugin-common": "^2.2.1",
Additional context Probably related to this commit: https://github.com/dotansimha/graphql-code-generator/commit/25cd11d01ec55d5c6a2a83ebd501abdd1ed2f248
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 17
- Comments: 25 (4 by maintainers)
After today’s update, nullable fields of objects are generated as
T | null | undefinedinstead ofT | null.I haven’t
maybeValueconfigured, but when I set it toT | null(which is the default), nullable fields are generated correctly. It is strange that setting default value explicitly changes the behavior.@gilgardosh This is still incorrect if you use custom
maybeValueIn mycodegen.ymlmaybeValueisT | undefined, so I shouldn’t have| nullin my types withpreResolveTypes: trueWe just released https://github.com/dotansimha/graphql-code-generator/releases/tag/%40graphql-codegen%2Ftypescript-operations%402.1.8 with a fix for this.
I created #7104, which uses
maybeValue: "T | null"as default. so no need to explicitly set maybeValue. Please let me know if this works for you.I upgraded to the latest version and after generating types I had 100+ type errors in my large TypeScript project (strict mode) due to the inclusion of
| undefinedin the maybe type.In my config script, like others have mentioned, I added
maybeValue: "T | null"(JS format, in.ymlyou can leave out the quotes) based on these docs, then all the type errors went away and normal behavior restored 👍maybeValueis begin used ontypescript-operations. IfpreResolveTypes: false, optional types are wrapped withMaybe, which is declared on typescript plugin asmaybeValue. But ifpreResolveTypes: true, thenMaybeis not used at all, and types are becoming nullable and undefinedable (depends onavoidOptionalsconfig).@lanwin is right regarding the current implementation referring only the
avoidOptionalsboolean config, it’s fixed hereKeeping open until we’ll fully resolve this one.
maybeValueshould be applied and interpolated also withpreResolveTypes: true. @gilgardosh can you please try to solve that?This is also what I expect. The current version breaks our code.
I also found that avoidOptionals behaves different when used with sub object instead of boolean. The following example you could try out at https://www.graphql-code-generator.com/ (v2.2.0).
Schema
Operations
codegen.yaml - boolean
Output:
codegen.yaml - subobject
Output:
I would expect this should output exact the same result? Shouldn’t it?
@n1ru4l sorry for the slow response! This fixes the problem for me.
I agree this makes sense to interpolate the
maybeValuewhenpreResolveTypesis used. @gilgardosh is working on a fix 😃I agree with @gilgardosh. Regardless of
preResolveTypesor usingMaybe<>,typescript-operationsshould use themaybeValue. The default value isT | null, notT | null | undefined.Hi @oceandrama , in this case, since
preResolveTypesis used, nomaybeshould be used in the generated types (neither the original nor the custommaybeValue)