【发布时间】:2022-01-10 14:36:21
【问题描述】:
我正在尝试调试 NodeJs 中的一个简单错误。错误:找不到模块“./schema/schema”。 我尝试使用字符串创建一个虚拟文件并尝试在我的项目根目录中使用,但出现了相同的错误。我试图删除大约 3x 的文件夹和文件,但错误仍然存在。 这是我的代码:
const express = require('express');
const expressGraphQL = require('express-graphql').graphqlHTTP;
const schema = require('./schema/schema');
const app = express();
app.use('/graphql', expressGraphQL({
schema: schema,
graphiql: true
}));
app.listen(4000, () => {
console.log('Listening');
});
我的架构文件
const graphql = require('axios');
const axios = require('axios');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
} = graphql
const CompanyType = new GraphQLObjectType({
name: 'Company',
fields: {
id: { type: GraphQLString },
name: { type: GraphQLString },
description: { type: GraphQLString }
}
});
const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLString},
firstName: { type: GraphQLString },
age: { type: GraphQLInt },
company: {
type: CompanyType,
resolve(parentValue, args) {
axios.get(`http://localhost:3000/companies/${parentValue.companyId}`).then(
(response) => response.data
);
}
}
}
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return axios.get(`http://localhost:3000/users/${args.id}`).then(
(response) => response.data
);
}
},
company: {
type: CompanyType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return axios.get(`https://localhost:3000/companies/${args.id}`).then(
(response) => response.data
);
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
我的项目是如何组织的:
我试图在另一台机器上克隆这个项目,但错误仍然存在。有人知道吗?文件中没有错别字,没有空格,我也尝试删除节点模块并运行yarn,但没有成功。
【问题讨论】:
-
两个答案都是正确的。我生成了
tsconfig.json,我必须使用命令ts-node server.ts。我是 Node 中 Ts 的新手,我一直使用 JS.Plus 我不得不将schema.ts更改为export default new GraphQLSchema
标签: javascript node.js typescript