dotansimha / dotansimha/graphql-code-generator

Optional input type filed with default is non-nullable in resolver

Open
#8,135 0 comments 2 reactions 0 assignees View on GitHub
core
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### 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;
b?: InputMaybe;
c: Scalars['Int'];
d: Scalars['Int'];
};
```

But generated resolver makes `b` not-nullable `RequireFields>`:
```
export type QueryResolvers = {
test?: Resolver>;
};
```

### Your Example Website or App

https://codesandbox.io/s/magical-chandrasekhar-3u6881

### Expected behavior

Generated resolver should be without `b` - `RequireFields>`:
```
export type QueryResolvers = {
test?: Resolver>;
};
```

### 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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.