dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.72k stars 1.31k forks source link

`verbatimModuleSyntax:true` in tsconfig.json causes generated files to fail type validation #10028

Closed the-simian closed 4 days ago

the-simian commented 4 days ago

Which packages are impacted by your issue?

@graphql-codegen/cli, @graphql-codegen/typed-document-node

Describe the bug

import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'; will throw an error

TypedDocumentNode is a type and must be imported using a type-only import when verbatimModuleSyntax is enabled.

This can be fixed by changing the line to import { type TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';

Your Example Website or App

https://github.com/dotansimha/graphql-code-generator

Steps to Reproduce the Bug or Issue

use this property and set it to true, which enforces stricter type checks in files, forcing the use of the keyword 'type' when something is a type

{
  "compilerOptions": {
      "verbatimModuleSyntax": true,
   }
}

then run graphql-codegen, the output fails.

Expected behavior

As a user I am expecting import { type TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'; but I am seeing import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';

the word 'type' is missing.

Screenshots or Videos

image vs image

Platform

Codegen Config File

No response

Additional context

Just put the word 'type' in the generated file and its fine!

the-simian commented 4 days ago

I believe this may be a duplicate issue of this solution, investigating: https://github.com/dotansimha/graphql-code-generator/issues/9595#issuecomment-1682041289

apologies if I jumped the gun here.

the-simian commented 4 days ago

Ok that was it.

import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
//...other config
  generates: {
    './db/__generated__/': {
      plugins: ['typescript', 'typescript-resolvers'],
      config: {
       //... other config.
        useTypeImports: true, //<--- this is the thing that fixes it.
      },
    },
}
export default config;