ค้นหาเว็บไซต์

typescript - ไม่พบโมดูลเมื่อใช้ graphql-code-generator


ฉันกำลังใช้ GraphQL codegen จาก https://www.graphql-code-generator.com/ ในโปรเจกต์ AWS CDK typescript Lambda ของฉัน ฉันต้องการใช้ปลั๊กอิน typescript-document-nodes หรือ typed-document-node (ฉันชอบ typed-document-node เพราะดูเหมือนว่าจะทันสมัยกว่า) ทุกครั้งที่ฉันลองใช้หนึ่งในปลั๊กอินเหล่านี้ ฉันจะได้รับข้อผิดพลาดในโปรเจกต์เกี่ยวกับโมดูลที่ไม่พบ (ปลั๊กอิน typescript-operations สร้างโค้ดตามที่คาดไว้)

codegen.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.

ปัญหา typescript-document-nodes:

โค้ดจากปลั๊กอินนี้ดูเหมือนว่าจะสร้างได้ดีในโปรเจกต์ของฉัน

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 ของฉันแบบนี้:

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

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

ฉันได้รับ:

ไม่สามารถหามอดูล '../../@types/graphql' ได้

เมื่อฉันนำเข้าประเภทที่สร้างจาก graphql.ts มันทำงานได้ดี อย่างไรก็ตามเมื่อฉันนำเข้า gql ฉันจะได้รับข้อผิดพลาดไม่พบโมดูล

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

ดังนั้นปัญหาทั้งสองดูเหมือนจะเกี่ยวข้องกับโมดูลที่ไม่พบ

บางทีปัญหานี้อาจเกี่ยวข้องกับไฟล์ tsconfig ของฉัน? ฉันเคยมีปัญหาเยอะในโปรเจกต์ CDK นี้ที่ไม่สามารถหามอดูลได้ ส่วนใหญ่เกี่ยวข้องกับ lambda layers แต่ปัญหานี้เป็นปัญหาเดียวที่เกี่ยวข้องกับ 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"
    ]
}

คำตอบ #1

ฉันได้เพิ่ม @graphql-typed-document-node/core ลงใน dependencies แทนที่จะเป็น devDependencies เพื่อแก้ปัญหานี้

คำตอบ #2

สิ่งที่ช่วยฉันเมื่อเผชิญกับปัญหานี้คือการลบโค้ดที่สร้างขึ้นโดยอัตโนมัติทั้งหมดและรันคำสั่งที่สร้างมันอีกครั้ง