Graphql-code-generator: Cannot read property 'name' of undefined - spread @include

Created on 6 Sep 2019  路  7Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
Get Error 'Cannot read property 'name' of undefined'

To Reproduce
Use a spread operator ...@include

Example:

query user($withUser: Boolean! = false) {
  ...@include(if: $withUser) {
    user {
      name
    }
  }  
}

Expected behavior
Codegen successfully creates query and query variables.

Environment:
Mac OS
"@graphql-codegen/cli": "1.7.0",
"@graphql-codegen/typescript": "1.7.0",
"@graphql-codegen/typescript-operations": "1.7.0",
"@graphql-codegen/typescript-react-apollo": "1.7.0"

bug core waiting-for-release

Most helpful comment

@dotansimha @n1ru4l - Thanks for the quick response. Codegen is fantastic.
The query works for me outside of codegen.
My use case is to selectively include peer (fragment level) fields based on boolean input variables.

Fragments are consumed by using the spread operator (...). All fields selected by the fragment will be added to the query field selection at the same level as the fragment invocation. This happens through multiple levels of fragment spreads.

Inline fragments may also be used to apply a directive to a group of fields. If the TypeCondition is omitted, an inline fragment is considered to be of the same type as the enclosing context.

from

https://github.com/graphql/graphql-spec/blob/master/spec/Section%202%20--%20Language.md

Thanks again
John

All 7 comments

Hmm interesting, thank you for reporting this @jcmoore0 .
The codegen doesn't support @inclulde and @skip directives support at the moment, but they should have been ignored and not throw an error.

Is this a correct GraphQL query? AFAIK it must be ... on TYPENAME @include(if: $condition)

@dotansimha @n1ru4l - Thanks for the quick response. Codegen is fantastic.
The query works for me outside of codegen.
My use case is to selectively include peer (fragment level) fields based on boolean input variables.

Fragments are consumed by using the spread operator (...). All fields selected by the fragment will be added to the query field selection at the same level as the fragment invocation. This happens through multiple levels of fragment spreads.

Inline fragments may also be used to apply a directive to a group of fields. If the TypeCondition is omitted, an inline fragment is considered to be of the same type as the enclosing context.

from

https://github.com/graphql/graphql-spec/blob/master/spec/Section%202%20--%20Language.md

Thanks again
John

@jcmoore0 Did not know that this is possible! Thank you for the resources 馃憤

@jcmoore0 I think I managed to reproduce and fix this error in: https://github.com/dotansimha/graphql-code-generator/pull/2532

Can you please try 1.7.1-alpha-2187dc6b.34?

@dotansimha - Sorry for the delay. 1.7.1-alpha-2187dc6b.34 works fantastic.
Great job!
Thanks for the fast response.

Fixed in 1.8.0 馃殌

Was this page helpful?
0 / 5 - 0 ratings