Graphql-code-generator: [typescript-graphql-files-modules] Cannot find module 'graphql'

Created on 17 Jul 2019  路  4Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
I'm attempting to use typescript-graphql-files-modules to generate types for my .graphql files.

The code is generated fine, however when I attempt to build the project, I now receive the error TS2307: Cannot find module 'graphql'.. I can confirm that graphql is installed and present in node_modules.

The error comes from the line import { DocumentNode } from 'graphql' inside the graphql module declaration.

To Reproduce
Here is a minimal reproduction. See the type error in types/graphql.d.ts

https://codesandbox.io/embed/graphql-codegen-issue-template-qqels

Expected behavior
The new type definitions should be usable without error.

Most helpful comment

@defrex I happens because of an issues with TypeScript. You can't have types and imports in a file with module declaration.
TypeScript module declarations must be in a .d.ts file, with no top level declarations. Also, note that if your schema have enums, you must use .ts extension because enums compiles into an object, while .d.ts can't have an output.

This works for me in the sandbox:

schema: schema.graphql
documents: document.graphql
generates:
  types/graphql.d.ts:
    - typescript-graphql-files-modules
  types/types.ts:
    - typescript

All 4 comments

@defrex can you please make sure that you have both graphql and @types/graphql installed?

@dotansimha I do indeed have both installed. Though it seems I forgot @types/graphql in the sandbox repro. I've fixed that now.

https://codesandbox.io/embed/graphql-codegen-issue-template-qqels

Why did this get closed? I have a pretty tight reproduction. It seems pretty likely to be a bug of some kind, or at very least a gotcha that's easy to trip on.

@defrex I happens because of an issues with TypeScript. You can't have types and imports in a file with module declaration.
TypeScript module declarations must be in a .d.ts file, with no top level declarations. Also, note that if your schema have enums, you must use .ts extension because enums compiles into an object, while .d.ts can't have an output.

This works for me in the sandbox:

schema: schema.graphql
documents: document.graphql
generates:
  types/graphql.d.ts:
    - typescript-graphql-files-modules
  types/types.ts:
    - typescript
Was this page helpful?
0 / 5 - 0 ratings