Graphql-code-generator: [typed-document-node] Error when fragment is defined in a separate file

Created on 21 Jul 2020  路  4Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

I'm using the typed-document-node plugin. When I define a fragment in a separate file, an error occurs. Fragments within the same file don't cause an error.

TypeError: Cannot read property 'replace' of undefined
        at /sandbox/node_modules/no-case/dist/index.js:36:58
        at Array.reduce (<anonymous>)
        at replace (/sandbox/node_modules/no-case/dist/index.js:36:15)
        at Object.noCase (/sandbox/node_modules/no-case/dist/index.js:14:26)
        at Object.pascalCase (/sandbox/node_modules/pascal-case/dist/index.js:20:22)
        at content.documents.map.docFile (/sandbox/node_modules/@graphql-codegen/typed-document
-node/dist/index.cjs.js:15:93)
        at Array.map (<anonymous>)
        at Object.plugin (/sandbox/node_modules/@graphql-codegen/typed-document-node/dist/index
.cjs.js:13:14)
        at executePlugin (/sandbox/node_modules/@graphql-codegen/core/index.cjs.js:36:35)
    TypeError: Cannot read property 'replace' of undefined
        at /sandbox/node_modules/no-case/dist/index.js:36:58
        at Array.reduce (<anonymous>)
        at replace (/sandbox/node_modules/no-case/dist/index.js:36:15)
        at Object.noCase (/sandbox/node_modules/no-case/dist/index.js:14:26)
        at Object.pascalCase (/sandbox/node_modules/pascal-case/dist/index.js:20:22)
        at content.documents.map.docFile (/sandbox/node_modules/@graphql-codegen/typed-document
-node/dist/index.cjs.js:15:93)
        at Array.map (<anonymous>)
        at Object.plugin (/sandbox/node_modules/@graphql-codegen/typed-document-node/dist/index
.cjs.js:13:14)
        at executePlugin (/sandbox/node_modules/@graphql-codegen/core/index.cjs.js:36:35)

It happens on this line: https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/typescript/typed-document-node/src/index.ts#L12, because operation.operation is undefined.

To Reproduce

CodeSandbox: https://codesandbox.io/s/cool-dijkstra-ghogd?file=/codegen.yml

Expected behavior

I should be able to define fragments in a separate file from the query that uses the fragment. It should probably skip over fragments altogether, since a TypedDocumentNode since I don't think we need a typed document nodes for fragments.

Environment:

  • OS: macOS 10.15.4
  • @graphql-codegen/1.17.0:
  • NodeJS: 14.4.0

Additional context

bug plugins waiting-for-release

Most helpful comment

Thanks again for reporting it, @TimonVS !
I fixed it (and actually, refactored the plugin to be more flexible and easier to integrate with other configurations) in: https://github.com/dotansimha/graphql-code-generator/pull/4419

Alpha is available: 1.17.4-alpha-b570ec6c.0, I tested it with your reproduction and it seems to work correctly!

All 4 comments

Another thing to note is that only the first operation is transformed to a typed document node: https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/typescript/typed-document-node/src/index.ts#L11. That explains why fragments work fine when they're defined in the same file as the query or mutation, as long as it's not defined first. Was this done intentionally or is the plan to generate typed documents for all operations in a single file?

Thank you for reporting it @TimonVS !
The typed-document-node plugin (and library) is fairly new, we are working to improve and stabilize it now.

Thanks again for reporting it, @TimonVS !
I fixed it (and actually, refactored the plugin to be more flexible and easier to integrate with other configurations) in: https://github.com/dotansimha/graphql-code-generator/pull/4419

Alpha is available: 1.17.4-alpha-b570ec6c.0, I tested it with your reproduction and it seems to work correctly!

Fixed in v1.17.4

Was this page helpful?
0 / 5 - 0 ratings