dotansimha/graphql-code-generator

Mention tsconfig-paths for codegen.ts?

Open

#8,838 创建于 2023年1月20日

在 GitHub 查看
 (7 评论) (6 反应) (0 负责人)TypeScript (10,341 star) (1,295 fork)batch import
help wanted

描述

Which packages are impacted by your issue?

No response

Describe the bug

If you are using a paths section in your tsconfig.json, the codegen.ts config will fail to load if it exports any code from your project. This can happen, for instance, if you are using Pathos, which recommends providing the scheme as a string to the config.

❯ npm run gql-codegen

> gql-codegen
> graphql-codegen

TypeScriptLoader failed to compile TypeScript:
Cannot find module '@lib/model-settings'
Require stack:
- /Users/artyom/code/windorg/woc/lib/graphql/schema/user.ts
- /Users/artyom/code/windorg/woc/lib/graphql/schema.ts
- /Users/artyom/code/windorg/woc/codegen.ts
- /Users/artyom/code/windorg/woc/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js
- ...

The solution is to add this section to your tsconfig.json:

  "ts-node": {
    "require": [
      "tsconfig-paths/register"
    ]
  }

And install npm i -D tsconfig-paths.

I suppose this could be mentioned in the getting-started guide, but I don't know where in particular.

Your Example Website or App

Steps to Reproduce the Bug or Issue

Expected behavior

Screenshots or Videos

Platform

  • OS: macOS
  • NodeJS: 16.18.0
  • graphql version: 16.6.0
  • @graphql-codegen/cli@2.16.2
  • @graphql-codegen/client-preset@1.2.6

Codegen Config File

import type { CodegenConfig } from '@graphql-codegen/cli'
import { printSchema } from 'graphql'
import { schema } from '@lib/graphql/schema'

const config: CodegenConfig = {
  schema: printSchema(schema),
  documents: ['{components,lib,pages}/**/!(*.graphql).{ts,tsx}'],
  generates: {
    './generated/graphql/': {
      preset: 'client',
      plugins: [],
    },
  },
}

export default config

Additional context

No response

贡献者指南