Graphql-code-generator: Aliasing `__typename` crashes codegen with a TypeError with `preResolveTypes: true`

Created on 16 Sep 2019  路  11Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
Aliasing __typename crashes with a TypeError with preResolveTypes: true, with preResolveTypes: false doesn't cause the crash.

To Reproduce
Steps to reproduce the behavior:


Here's an example:
https://codesandbox.io/s/graphql-codegen-issue-template-x2gtb

  1. My GraphQL schema:
  type A {
    string: String!
  }
  type Query {
    a: [A!]!
  }
  1. My GraphQL operations:
  fragment Fraggy on A {
    # Aliasing '__typename' causes a TypeError with preResolveTypes: true
    type: __typename
    string
  }
  query QQ {
    a {
      ...Fraggy
    }
  }
  1. My codegen.yml config file:
{ preResolveTypes: true }

Expected behavior
Should work, like it does with { preResolveTypes: false }

Environment:

  • OS: mac
  • @graphql-codegen/typescript:
  • NodeJS: 10

Additional context

bug plugins waiting-for-release

Most helpful comment

All issues I was aware of have been fixed with the latest canary. Have you scheduled a release sometime soon?

All 11 comments

@merisbahti Could you share the whole error stack trace?

Running node index.js in the sandbox yields:

/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/selection-set-to-object.js:359
            const baseType = utils_1.getBaseType(fieldObj.type);
                                                          ^

TypeError: Cannot read property 'type' of undefined
    at fields.map.aliasedField (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/selection-set-to-object.js:359:59)
    at Array.map (<anonymous>)
    at TypeScriptSelectionSetToObject.buildAliasedPrimitiveFieldsWithoutPick (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/selection-set-to-object.js:357:23)
    at TypeScriptSelectionSetToObject.buildSelectionSetString (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/selection-set-to-object.js:316:46)
    at possibleTypes.map.type (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/selection-set-to-object.js:224:25)
    at Array.map (<anonymous>)
    at TypeScriptSelectionSetToObject.get string [as string] (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/selection-set-to-object.js:217:14)
    at TypeScriptDocumentsVisitor.FragmentDefinition (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/dist/commonjs/base-documents-visitor.js:78:39)
    at Object.visit (/sandbox/node_modules/graphql/language/visitor.js:242:26)
    at exports.plugin (/sandbox/node_modules/@graphql-codegen/typescript-operations/dist/commonjs/index.js:14:37)

I'm working now on fixes for selection set issues, I'll try to fix that as well.
Thanks @merisbahti !

Thank you.

@merisbahti
Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2540
Can you please check the latest alpha? 1.7.1-alpha-4e73dea2.5 (it also includes some other changes related to selection sets)

@merisbahti
Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2540
Can you please check the latest alpha? 1.7.1-alpha-4e73dea2.5 (it also includes some other changes related to selection sets)

Excellent, this fixed another issue I was having! Thank you 馃憤

@merisbahti thanks for the feedback. I'm keeping open until we'll release it.

All issues I was aware of have been fixed with the latest canary. Have you scheduled a release sometime soon?

Great, thanks @merisbahti . We plan to release 1.8.0 today or tomorrow (I just plan to include some more bug fixes).

Fixed in 1.8.0 馃殌

Was this page helpful?
0 / 5 - 0 ratings