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

[typescript] non nullable graphql inputs with default values are not optional as ts types #4273

Closed
bzvikler opened this issue Jun 22, 2020 · 3 comments
Labels
plugins waiting-for-release Fixed/resolved, and waiting for the next stable release

Comments

@bzvikler
Copy link

bzvikler commented Jun 22, 2020

Describe the bug
Currently if a property within the gql schema is set as non nullable, but has a default value assigned to it, the generated typescript property will be set as required.

To Reproduce
Steps to reproduce the behavior:

  1. My GraphQL schema:
input App {
    id: ID!
    displayName: String
    users: [String!]! = ["Hey", "hello"]
}
  1. Generated type
export type App = {
  id: Scalars['ID'];
  displayName?: Maybe<Scalars['String']>;
  users: Array<Scalars['String']>;
};
  1. My codegen.yml config file:
schema: schema.graphql
documents: document.graphql
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-operations

Expected behavior
If there is a provided default value, the generated ts type should allow for a value of undefined, as this is when the default value would be applied.

Environment:

  • running on the code sandbox

Additional context
The issue arose when I was trying to integrate with a gql schema that indicated an array type with a default value. I wanted the default value, but due to this issue, I had to explicitly enter that value, as opposed to just omitting the type and letting the default handle it.

@dotansimha
Copy link
Owner

Fixed in @graphql-codegen/typescript@1.17.10

@mhart
Copy link

mhart commented Sep 29, 2020

Hmmm, this has caused a bunch of incompatible changes in our codebase from a patch release.

Making these optional might make sense on a client, but not on a server – because they're guaranteed to have values. What's the best way to get around this issue?

@dotansimha
Copy link
Owner

@mhart I think you are right, can you please report this as separate issue? we'll look into it :) maybe this should be configurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugins waiting-for-release Fixed/resolved, and waiting for the next stable release
Projects
None yet
Development

No branches or pull requests

3 participants