graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

avoidOptionals inputValue as false not producing optional fields

Open cdaringe opened this issue 5 years ago • 3 comments

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

export type FindUserQueryVariables = Exact<{
  userId: Maybe<Scalars['ID']>;
}>;

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:

schema {
  query: Query
}

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

interface Node {
  id: ID!
}

type User implements Node {
  id: ID!
}
  1. My GraphQL operations:
query findUser($userId: ID) {
  user(id: $userId) {
    id
  }
}
  1. My codegen.yml config file:
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<Int> field:

expected:

export type FindUserQueryVariables = Exact<{
  userId?: Maybe<Scalars['ID']>;
}>;

actual:

export type FindUserQueryVariables = Exact<{
  userId: Maybe<Scalars['ID']>;
}>;

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

cdaringe avatar Aug 20 '21 16:08 cdaringe

Hello, are there any updates here? Or other ways to solve this issue?

jtsmills avatar Jul 29 '24 19:07 jtsmills

This no longer reproduces on current master (b71b9d5). With avoidOptionals: { inputValue: false } (any object value) the generated input field is userId?: …; it only becomes required with avoidOptionals: true. Probably safe to close.

nomadical avatar Nov 25 '26 01:11 nomadical

There has been a major change and typescript-operations no longer need typescript to work.

avoidOptionals changed for typescript-operations here. So, most likely this is not an issue anymore like @nomadical analysed.

Feel free to open a new issue if it's still a problem 🙂

eddeee888 avatar Nov 25 '26 11:11 eddeee888