繁体   English   中英

使用 graphql-code-generator 时找不到模块

[英]module not found when using graphql-code-generator

我正在使用来自https://www.graphql-code-generator.com/的 GraphQL 代码生成器。 在我的 AWS CDK typescript Lambda 项目中。 我想使用 typescript-document-nodes 或 typed-document-node 插件。 (我更喜欢 typed-document-node 因为它看起来更现代)。 每当我尝试其中一个插件时,我的项目中都会出现与未找到的模块相关的错误。 (插件 typescript-operations 正在生成预期的代码)

代码生成.yml:

watch: false
watchConfig:
  usePolling: false
  interval: 1000
overwrite: true
schema:
  - xxxxxxxxx:
      headers:
        'x-api-key': xxxxxx
  - "awsAppsyncDirectives.graphql"
documents:
- 'src/graphql/*.query.graphql'
- 'src/graphql/*.fragment.graphql'
- 'src/graphql/*/*.query.graphql'
- 'src/graphql/*/*.fragment.graphql'
generates:
  src/@types/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-document-nodes"
      - "typed-document-node"

类型化文档节点问题:

生成的代码(graphql.ts)看起来有问题在我的编辑器中,我看到以下错误:

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


Cannot find module '@graphql-typed-document-node/core' or its corresponding type declarations.

使用 codegen cli 生成代码时,我也会收到相同的错误:

[12:26:05 PM] File change detected. Starting incremental compilation...

src/@types/graphql.ts:1:51 - error TS2307: Cannot find module '@graphql-typed-document-node/core' or its corresponding type declarations.

1 import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
[12:26:05 PM] Found 1 error. Watching for file changes.

打字稿文档节点问题:

这个插件的代码似乎在我的项目中生成得很好

export const ListAuthorizationTwinfields = gql`
    query listAuthorizationTwinfields($filter: ModelAuthorizationTwinfieldFilterInput) {
  listAuthorizationTwinfields(filter: $filter) {
    items {
      accessToken
      accessTokenValidatedUntil
      active
      administrationId
      cluster
      createdAt
      debtorsImported
      debtorsStarted
      firstBankStatement
      id
      incremental
      invoicesImported
      invoicesStarted
      lastExecutedCallback
      memosImportedAt
      memosStartedAt
      officeCode
      officeName
      refreshToken
      transactionCodes
      transactionsStarted
      transactionsImported
      updatedAt
    }
  }
}
    `;

但是,当从我的 Lambda function 运行代码时,如下所示:

import { ListAuthorizationTwinfields } from "../../@types/graphql";

    data = await graphql.query({
        query: ListAuthorizationTwinfields,
        variables: variables
    }, graphqlConfig);

我得到:

找不到模块'../../@types/graphql'

当我从 graphql.ts 导入生成的类型时,它工作正常。 但是,在导入 gql 时,我得到了找不到模块错误

====================================

因此,这两个问题似乎都与未找到模块有关。

也许这个问题与我的 tsconfig 文件有关? 我在这个 CDK 项目中遇到了很多无法找到模块的问题,其中大部分与 lambda 层有关。 但是这个是唯一一个和codegen相关的。

{
    "compilerOptions": {
        "target": "ES2021",
        "module": "commonjs",
        "moduleResolution": "node",
        "lib": [
            "ES2021",
            "DOM"
        ],
        "esModuleInterop": true,
        "declaration": true,
        "strict": true,
        "noImplicitAny": false,
        "noImplicitThis": false,
        "strictNullChecks": true,
        "alwaysStrict": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": false,
        "inlineSourceMap": true,
        "inlineSources": true,
        "experimentalDecorators": true,
        "strictPropertyInitialization": false,
        "useUnknownInCatchVariables": false,
        "typeRoots": [
            "./node_modules/@types",
            "./src/@types"
        ]
    },
    "include": [
        "./src/lambda",
        "./src/layers/helper/nodejs/node_modules",
    ],
    "exclude": [
        "node_modules",
        "cdk.out"
    ]
}

我将@graphql-typed-document-node/core添加到我的dependencies项而不是devDependencies来解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM