【问题标题】:could not find declaration file for 'merge-graphql-schemas' TYPESCRIPT找不到“merge-graphql-schemas”TYPESCRIPT 的声明文件
【发布时间】:2018-12-09 08:48:23
【问题描述】:

除非我删除 noImplicitAny,否则我无法编译 typescript

我得到了这个错误,我正在使用来自 npm 的 merge-graphql-schemas 库

    TSError: ⨯ Unable to compile TypeScript:
src/utils/genSchema.ts(1,44): error TS7016: Could not find a declaration file for module 'merge-graphql-schemas'. '/home/user/Documents/voting/node_modules/merge-graphql-schemas/dist/index.cjs.js' implicitly has an 'any' type.
  Try `npm install @types/merge-graphql-schemas` if it exists or add a new declaration (.d.ts) file containing `declare module 'merge-graphql-schemas';

在我的 src->types->merge-graphql-schemas.d.ts

声明模块“merge-graphql-schemas”;

我的 tsconfig 是这样的

    {
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",

    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.tsx", "./src/**/*.ts"]
}

我是不是应该专门做点什么? 我将 lib 从 npm 更改为 schemaglue,即使它也没有类型,所以我做了同样的事情,但对于 schemaglue,我仍然得到相同的错误。 也许我在 tsconfig 中遗漏了什么?

这就是我使用 merge-schema-graphql 的原因

    import { mergeTypes, mergeResolvers } from "merge-graphql-schemas";
import * as path from "path";
import * as fs from "fs";
import { makeExecutableSchema } from "graphql-tools";
import * as glob from "glob";

export const genSchema = () => {
  const pathToModules = path.join(__dirname, "../modules");
  const graphqlTypes = glob
    .sync(`${pathToModules}/**/*.graphql`)
    .map(x => fs.readFileSync(x, { encoding: "utf8" }));

  const resolvers = glob
    .sync(`${pathToModules}/**/resolvers.?s`)
    .map(resolver => require(resolver).resolvers);

  return makeExecutableSchema({
    typeDefs: mergeTypes(graphqlTypes),
    resolvers: mergeResolvers(resolvers)
  });
};

礼貌 Youtube Benawad graphql-ts-server-boilerplate

【问题讨论】:

    标签: typescript graphql


    【解决方案1】:

    我遇到了同样的问题! 我通过移动来修复它

    import { fileLoader, mergeTypes, mergeResolvers } from 'merge-graphql-schemas'
    

    到之后

    import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
    import { makeExecutableSchema } from 'graphql-tools'
    

    我不完全确定为什么会发生这种情况,但我相信这是由于一些不稳定的依赖关系。

    【讨论】:

      【解决方案2】:

      这里是问题:https://github.com/okgrow/merge-graphql-schemas/issues/154

      为简单起见,您可以创建一个global.d.ts 文件并放入以下声明:

      declare module 'merge-graphql-schemas' {
        export function fileLoader(...args: any[]): any;
        export function mergeResolvers(...args: any[]): any;
        export function mergeTypes(...args: any[]): any;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-28
        • 2017-02-16
        • 2020-08-02
        • 1970-01-01
        • 2019-03-06
        • 2019-12-29
        • 1970-01-01
        • 1970-01-01
        • 2021-08-24
        相关资源
        最近更新 更多