如何修复这个 typescript 和 graphql 错误?
Posted
技术标签:
【中文标题】如何修复这个 typescript 和 graphql 错误?【英文标题】:How do I fix this typescript and graphql error? 【发布时间】:2020-10-06 10:50:13 【问题描述】:在我的索引文件中,我尝试使用 graphql-tools 方法 makeExicutableSchema
和 buildSchema
将我所有的 typedef 和解析器聚合到一个 GraphQLSchema[]
中,然后从 graphql-yoga 放入 GraphQLServer。
import importSchema from "graphql-import";
import GraphQLServer from 'graphql-yoga';
import * as path from 'path';
import * as fs from 'fs';
import createTypeOrmConnection from "./utils/createTypeOrmConn";
import mergeSchemas, makeExecutableSchema from "graphql-tools";
import GraphQLSchema from 'graphql';
export const startServer = async () =>
const schemas: GraphQLSchema[] = [];
const folders = fs.readdirSync(path.join(__dirname, "./modules"));
folders.forEach(folder =>
const resolvers = require(`./modules/$folder/resolvers`);
const typeDefs = importSchema(
path.join(__dirname, `./modules/$folder/schema.graphql`)
);
schemas.push(makeExecutableSchema(
resolvers,
typeDefs
));
);
const server = new GraphQLServer(
schema: mergeSchemas( schemas )
);
await createTypeOrmConnection();
const app = await server.start(port: process.env.NODE_ENV === "test" ? 0 : 4000)
console.log('Server is running on http://localhost:4000')
return app;
;
这是schema: mergeSchema( schemas )
处的错误:
Type 'import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/graphql/type/schema").GraphQLSchema' is not assignable to type 'import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/apollo-utilities/node_modules/graphql/type/schema").GraphQLSchema'.
The types returned by 'getQueryType()' are incompatible between these types.
Type 'Maybe<GraphQLObjectType<any, any>>' is not assignable to type 'Maybe<GraphQLObjectType<any, any, [key: string]: any; >>'.
Type 'GraphQLObjectType<any, any>' is not assignable to type 'Maybe<GraphQLObjectType<any, any, [key: string]: any; >>'.
Types of property 'isTypeOf' are incompatible.
Type 'import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/graphql/jsutils/Maybe").Maybe<import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/graphql/type/definition").GraphQLIsTypeOfFn<any, any>>' is not assignable to type 'import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/apollo-utilities/node_modules/graphql/tsutils/Maybe").default<import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/apollo-utilities/node_modules/graphql/type/definition").GraphQLIsTypeOfFn<any, any>>'.
Type 'GraphQLIsTypeOfFn<any, any>' is not assignable to type 'Maybe<GraphQLIsTypeOfFn<any, any>>'.
Types of parameters 'info' and 'info' are incompatible.
Property 'cacheControl' is missing in type 'import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/apollo-utilities/node_modules/graphql/type/definition").GraphQLResolveInfo' but required in type 'import("/Users/jackrisse/Projects/project-da-league/backend/node_modules/graphql/type/definition").GraphQLResolveInfo'.
【问题讨论】:
您可能正在使用两个不同版本的 GraphQL 库。这是 JS 包中的一个基本缺陷,很少讨论。您可以将 GraphQL 的版本固定在 yarn 或 npm 中。 【参考方案1】:我通过将架构分配给任何类型找到了解决方法
const schema: any = mergeSchemas( schema );
【讨论】:
【参考方案2】:我遇到了同样的问题,并通过将 package.json 中的 graphql 版本更改(降级)为与 @types/graphql 相同来修复它。
正如 Herku(在评论部分)所说:
您可能正在使用两个不同版本的 GraphQL 库。这是 JS 包的根本缺陷
我认为这是正确的答案。
我的 package.json 的一部分
【讨论】:
以上是关于如何修复这个 typescript 和 graphql 错误?的主要内容,如果未能解决你的问题,请参考以下文章
typescript eventTarget dataset 如何修复报错?
如何修复已使用 react 和 typescript 声明的错误标识符?
如何修复 Typescript 中的“以下属性中缺少类型''...”错误?
如何修复'Typescript“正在进行类型检查......”花费太长时间'?