graphql-code-generator
graphql-code-generator copied to clipboard
Optional input type filed with default is non-nullable in resolver
Describe the bug
For schema:
type Query {
test(
a: Int # can be null
b: Int = 1 # can be null if user pass null explicitly
c: Int! # can't be null
d: Int! = 1 # can't be null
): Int!
}
This will pass validation and b will not be set to 1, which will result in unexpected null in resolver while generated types will state that it's a number
query test {
test(a: null, b: null, c: 1, d: 1)
}
Note: avoidOptionals.defaultValue is set to true to generate correct QueryTestArgs related issue #8129
Generated arg types are correct:
export type QueryTestArgs = {
a?: InputMaybe<Scalars['Int']>;
b?: InputMaybe<Scalars['Int']>;
c: Scalars['Int'];
d: Scalars['Int'];
};
But generated resolver makes b not-nullable RequireFields<QueryTestArgs, 'b' | 'c' | 'd'>>:
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
test?: Resolver<ResolversTypes['Int'], ParentType, ContextType, RequireFields<QueryTestArgs, 'b' | 'c' | 'd'>>;
};
Your Example Website or App
https://codesandbox.io/s/magical-chandrasekhar-3u6881
Expected behavior
Generated resolver should be without b - RequireFields<QueryTestArgs, 'c' | 'd'>>:
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
test?: Resolver<ResolversTypes['Int'], ParentType, ContextType, RequireFields<QueryTestArgs, 'c' | 'd'>>;
};
Platform
"graphql": 16.5.0 "@graphql-codegen/cli": "2.9.1" "@graphql-codegen/typescript": "2.7.2" "@graphql-codegen/typescript-operations": "2.5.2" "@graphql-codegen/typescript-resolvers": "2.7.2"
Codegen Config File
schema: schema.graphql
documents: document.graphql
generates:
types.ts:
plugins:
- typescript
- typescript-operations
- typescript-resolvers
config:
avoidOptionals:
defaultValue: true