typescript - modul tidak ditemukan saat menggunakan graphql-code-generator
Saya sedang menggunakan GraphQL codegen dari https://www.graphql-code-generator.com/ di proyek Lambda AWS CDK dengan TypeScript saya. Saya ingin menggunakan plugin typescript-document-nodes atau typed-document-node. (Saya lebih suka typed-document-node karena sepertinya itu lebih modern). Setiap kali saya mencoba salah satu plugin ini, saya mendapatkan error di proyek saya terkait modul yang tidak ditemukan. (Plugin typescript-operations menghasilkan kode yang diharapkan)
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"
Masalah node-dokumen-bertulis:
Ada yang salah dengan kode yang dihasilkan (graphql.ts) Di editor saya saya melihat kesalahan berikut:
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
Cannot find module '@graphql-typed-document-node/core' or its corresponding type declarations.
Saat menghasilkan kode dengan codegen cli saya juga mendapatkan kesalahan yang sama:
[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.
Masalah typescript-document-nodes:
Kode dari plugin ini tampaknya berjalan dengan baik di proyek saya
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
}
}
}
`;
Namun, ketika menjalankan kode dari fungsi Lambda saya seperti ini:
import { ListAuthorizationTwinfields } from "../../@types/graphql";
data = await graphql.query({
query: ListAuthorizationTwinfields,
variables: variables
}, graphqlConfig);
Aku dapat:
Tidak dapat menemukan modul '../../@types/graphql'
Ketika saya mengimpor tipe yang dihasilkan dari graphql.ts, itu bekerja dengan baik. Namun ketika mengimpor gql saya mendapatkan kesalahan tidak dapat menemukan modul
==================================
Jadi kedua masalah tampaknya terkait dengan modul yang tidak ditemukan.
Mungkin masalah ini terkait dengan file tsconfig saya? Saya mengalami banyak masalah dalam proyek CDK ini karena tidak bisa menemukan modul, sebagian besar terkait dengan lambda layers. Tapi ini adalah satu-satunya yang terkait dengan 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"
]
}
Jawaban #1
Saya menambahkan @graphql-typed-document-node/core ke dependencies saya alih-alih devDependencies untuk menyelesaikan masalah ini.
Jawaban #2
Yang membantu saya saat menghadapi masalah ini adalah menghapus semua kode yang dibuat otomatis dan menjalankan ulang perintah yang menghasilkannya.