Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputMaybe should explicitly include undefined #10005

Open
ericbf opened this issue Jun 14, 2024 · 4 comments
Open

InputMaybe should explicitly include undefined #10005

ericbf opened this issue Jun 14, 2024 · 4 comments

Comments

@ericbf
Copy link

ericbf commented Jun 14, 2024

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

  • OS: macOS
  • NodeJS: 18.20.3
  • graphql version: ^16.2.0
  • @graphql-codegen/add version: ^5.0.0
  • @graphql-codegen/cli version: ^4.0.1
  • @graphql-codegen/typescript version: ^4.0.1
  • @graphql-codegen/typescript-operations version: ^4.0.1

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.

@ericbf ericbf changed the title InputMaybe should explicitly include undefined if avoidOptionals is not set InputMaybe should explicitly include undefined Jun 14, 2024
@ericbf ericbf changed the title InputMaybe should explicitly include undefined InputMaybe should explicitly include undefined Jun 14, 2024
@eddeee888
Copy link
Collaborator

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
Copy link
Collaborator

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

@ericbf
Copy link
Author

ericbf commented Jul 1, 2024

@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.

@eddeee888 eddeee888 reopened this Jul 2, 2024
@ericbf ericbf changed the title InputMaybe should explicitly include undefined InputMaybe should explicitly include undefined (client preset) Jul 2, 2024
@ericbf ericbf changed the title InputMaybe should explicitly include undefined (client preset) InputMaybe should explicitly include undefined Jul 2, 2024
@DB-Vadim-Wagner
Copy link

We are having the same Problem.
Being able to set inputMaybeValue with the client preset would solve the problem for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants