dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.72k stars 1.31k forks source link

`InputMaybe` should explicitly include `undefined` #10005

Open ericbf opened 3 weeks ago

ericbf commented 3 weeks ago

Which packages are impacted by your issue?

@graphql-codegen/add @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-operations

Describe the bug

When using the typescript compiler option "exactOptionalPropertyTypes": true, the fact that InputMaybe<T> does not explicitly include undefined makes it hard to use as you can no longer pass optional values to query variables.

InputMaybe should explicitly include undefined to allow passing optional values to optional variables in queries.

Your Example Website or App

https://stackblitz.com/edit/github-txjvxl?file=query.ts

Steps to Reproduce the Bug or Issue

  1. Open repro
  2. Look at compilation error (if it doesn't show in the editor, run npx tsc to see it)

Expected behavior

No compilation error. Since input variable id is optional, it should allow an optional value to be set to it.

Screenshots or Videos

No response

Platform

Codegen Config File

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "schema.graphql",
  documents: "document.graphql",
  generates: {
    "types.ts": { plugins: ["typescript", "typescript-operations"] },
  },
};

export default config;

Additional context

There’s an option to customize the InputMaybe type, but when using some presents, like the client preset, the option is not available. I feel codegen should work in a way compatible with at least this relatively non-niche typescript config.

eddeee888 commented 1 week ago

Hi @ericbf

inputMaybeValue can be used to add undefined like this:

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file.ts': {
      plugins: ['typescript'],
      config: {
        inputMaybeValue: 'T | null | undefined' // Add undefined to inputMaybeValue
      }
    }
  }
}
eddeee888 commented 1 week ago

I'll close this issue, but if this doesn't answer your question, please @ me and let me know to re-open 🙂

ericbf commented 1 week ago

@eddeee888 I should have specified I’m using the client preset, and this option doesn’t work when using the client preset. I’ve updated the issue description accordingly.