Graphql-code-generator: Handle lists and non-null properly

Created on 9 Dec 2017  路  8Comments  路  Source: dotansimha/graphql-code-generator

Hello,

Right now gql-gen doesn't check if ! (non-nullable) applies to a list or elements inside the list:

https://github.com/dotansimha/graphql-code-generator/blob/f3f6e0aa4d482bf407e507267424e363d1d32758/packages/graphql-codegen-core/src/schema/resolve-type.ts#L10

As a result, the list is considered required even when it isn't:

GraphQL type | Expected Typescript Type | Actual Generated Type
------------ | ------------- | -------------
[String] | (string \| null)[] \| null | string[] \| null
[String]! | (string \| null)[] | string[]
[String!] | string[] \| null | string[]
[String!]!| string[] | string[]

More generally, GraphQL allows arbitrary nesting of list and non-null modifiers according to http://graphql.org/learn/schema/#lists-and-non-null .

Most helpful comment

Fixed in 0.8.16. Thanks @FredyC !

All 8 comments

I ran into the same problem.

Yea just encountered this too. Should be pretty simple fix seeing that code, something like lastIndexOf(']!') should be sufficient in my opinion.

Unfortunately, it feels like @dotansimha is rather busy lately, he is not responding to issues much. Looks like custom fork will be necessary, but that's going to be a pain considering monorepo structure.

I made the PR. It's a working solution, I have a rather big schema and nothing was broken by that change.

@FredyC Hi, I'd like to try your PR. Could you tell me a little bit about how to use it in my project.

package.json

"graphql-code-generator": "git://github.com/FredyC/graphql-code-generator.git#FredyC-patch-1",

I executed yarn install, but gql-gen command was not created in ./node_modules/.bin/.

@shinyaohira I don't think you can do that because of a monorepo structure which includes multiple packages that need to be built. However, looks like that PR will be merged soon, just be a little patient :)

Fixed in 0.8.16. Thanks @FredyC !

Also, @shinyaohira, @FredyC is right, you can't use it from git. But you can compile it locally and try to use it this way...

@FredyC @dotansimha Thank you 馃檶

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamdanthedev picture iamdanthedev  路  3Comments

steebchen picture steebchen  路  3Comments

quolpr picture quolpr  路  3Comments

fvisticot picture fvisticot  路  3Comments

NickClark picture NickClark  路  3Comments