dotansimha / dotansimha/graphql-code-generator

avoidOptionals inputValue as false not producing optional fields

Open
#6,521 1 comment 19 reactions 0 assignees View on GitHub
core help wanted kind/enhancement
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Describe the bug**

In the following live demo:

- observe an optional GQL query input, `userId: ID`
- you can see that we are `avoidOptionals: { inputValue: false }` for that value
- the output **does avoid** optionals, despite being instructed _not to_. you can see the following output:

![image](https://user-images.githubusercontent.com/1003261/130266032-6fa99a99-52af-4df4-9aec-73e7d12d4182.png)

```ts
export type FindUserQueryVariables = Exact<{
userId: Maybe;
}>;
```

Interestingly, if you flip `object: false` as well, suddenly the correct behavior takes for `inputValue: false`. Perhaps these are unintentionally coupled?

https://www.graphql-code-generator.com/docs/plugins/typescript-operations

**To Reproduce**

Codesandbox has been down for me. The schema, query, & config are listed below. The repro is quite simple :)

1. My GraphQL schema:

Stripped down version of the homepage demo:

```graphql
schema {
query: Query
}

type Query {
user(id: ID): User
}

interface Node {
id: ID!
}

type User implements Node {
id: ID!
}
```

2. My GraphQL operations:

```graphql
query findUser($userId: ID) {
user(id: $userId) {
id
}
}
```

3. My `codegen.yml` config file:

```yml
generates:
operations-types.ts:
plugins:
- typescript
- typescript-operations
config:
avoidOptionals:
defaultValue: true
field: true
object: true # toggle me!
inputValue: false

```

**Expected behavior**

To produce an optional value for the `userId?: Maybe` field:

**expected**:

```ts
export type FindUserQueryVariables = Exact<{
userId?: Maybe;
}>;
```

**actual**:

```ts
export type FindUserQueryVariables = Exact<{
userId: Maybe;
}>;
```

**Environment:**

latest, deployed on graphql-code-generator website

**Additional context**

This PR https://github.com/dotansimha/graphql-code-generator/pull/5113, in the bottom comment, seems to suggest that this design is intentional. However, `object` seems orthogonal to `inputValue` for `avoidOptionals` configuration, and produces an unexpected result. If they are not orthogonal, then `inputValue` should take precedence over `object`

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.