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

Resolvers input subfields with defaults resolve into nullable

Open gilgardosh opened this issue 3 years ago • 6 comments

Describe the bug

Resolvers input subfields with defaults resolve into nullable

Your Example Website or App

https://stackblitz.com/edit/node-ve8vhp?file=schema.graphql

Steps to Reproduce the Bug or Issue

typescript-resolvers:
overwrite: true
schema:
  - schema.graphql

documents: null
generates:
  generated-types.ts:
    plugins:
      - 'typescript'
      - 'typescript-resolvers'

and this is the schema:

type User {
  name: String
}

input GetUserArgs {
   offset: Int = 0
}

type Query {
  getUsers(getUsersArgs: GetUserArgs): [User]
}

Expected behavior

for offset arg to be wrapped with RequireFields in the. resolver type

Screenshots or Videos

No response

Platform

  • OS: macOS
  • NodeJS: 16.14.2
  • graphql version: 16.0.1
  • "@graphql-codegen/cli": "2.9.0"
  • "@graphql-codegen/typescript": "2.7.2"
  • "@graphql-codegen/typescript-resolvers": "2.7.2"

Codegen Config File

No response

Additional context

No response

gilgardosh avatar Jul 20 '22 12:07 gilgardosh

The reason why the offset is nullable.
Kind is import from graphql

image

xiaoliu-heng avatar Jul 21 '22 10:07 xiaoliu-heng

weird. it's undefined only when I debug it.

xiaoliu-heng avatar Jul 21 '22 13:07 xiaoliu-heng

the real reason:

    const addOptionalSign =
      !this.config.avoidOptionals.inputValue &&
      (originalFieldNode.type.kind !== Kind.NON_NULL_TYPE ||
        (!this.config.avoidOptionals.defaultValue && node.defaultValue !== undefined));

// node.defaultValue == { kind: 'IntValue', value: '0' }

https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/typescript/typescript/src/visitor.ts#L297

seems that it's the expected behavior

xiaoliu-heng avatar Jul 21 '22 13:07 xiaoliu-heng

related: #5113

xiaoliu-heng avatar Jul 21 '22 13:07 xiaoliu-heng

generates:
  types.ts:
    plugins:
      - typescript
    config:
      avoidOptionals: 
        defaultValue: true

set avoidOptionals.defaultValue to true

xiaoliu-heng avatar Jul 21 '22 13:07 xiaoliu-heng

If user pass null as argument explicitly, it can be null, so it works as it should be. See #8129

ziimakc avatar Jul 22 '22 17:07 ziimakc