【问题标题】:src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarationssrc/index.ts:12:22 - 错误 TS2307: 找不到模块 './schema.graphql' 或其对应的类型声明
【发布时间】:2021-12-21 20:15:24
【问题描述】:

您好,我想导入一个 graphql 文件。为此我已经安装了 babel-plugin-import-graphql 包。但由于某种原因我收到错误

\node_modules\ts-node\src\index.ts:750
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations.

12 import typeDefs from "./schema.graphql";
                        ~~~~~~~~~~~~~~~~~~
 

如何进行导入

import typeDefs from "./schema.graphql";

graphql.d.ts

declare module "*.graphql" {
  import { DocumentNode } from "graphql";

  const value: DocumentNode;
  export = value;
}

.babelrc

{
  "plugins": ["import-graphql"]
}

完整代码可以查看here

【问题讨论】:

  • ...您正在学习教程...?谁告诉你应该那样做的? graphql schema != TS type defs ... 可能您正在从 graphql 架构中寻找 TS 类型生成器,请使用 google/search
  • 谁说不应该这样做?

标签: node.js typescript import graphql


【解决方案1】:

将文件从src/@types/graphql.d.ts重命名为src/@types/index.d.ts,保持内容不变,然后重启TS服务器(或者只是重新加载IDE)。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2021-08-27
    • 2022-10-17
    • 2021-05-14
    • 2021-04-08
    • 2022-10-06
    • 2021-04-15
    • 2023-01-31
    • 2020-11-17
    相关资源
    最近更新 更多