我试图将我的代码分解为模块,现在出现以下错误:
Error: Query root type must be provided.
at assertValidSchema (C:\Users\aironsid\Documents\tatooify\server\node_modules\graphql\type\validate.js:71:11)
at assertValidExecutionArguments (C:\Users\aironsid\Documents\tatooify\server\node_modules\graphql\execution\execute.js:153:35)
at executeImpl (C:\Users\aironsid\Documents\tatooify\server\node_modules\graphql\execution\execute.js:101:3)
at Object.execute (C:\Users\aironsid\Documents\tatooify\server\node_modules\graphql\execution\execute.js:63:63)
at Object.generateSchemaHash (C:\Users\aironsid\Documents\tatooify\server\node_modules\apollo-server-core\dist\utils\schemaHash.js:15:32)
at ApolloServer.generateSchemaDerivedData (C:\Users\aironsid\Documents\tatooify\server\node_modules\apollo-server-core\dist\ApolloServer.js:269:41)
at new ApolloServerBase (C:\Users\aironsid\Documents\tatooify\server\node_modules\apollo-server-core\dist\ApolloServer.js:161:38)
at new ApolloServer (C:\Users\aironsid\Documents\tatooify\server\node_modules\apollo-server-express\dist\ApolloServer.js:60:9)
at Object.<anonymous> (C:\Users\aironsid\Documents\tatooify\server\index.js:11:16)
这是我的index.js
const express = require('express');
const {
ApolloServer,
} = require('apollo-server-express');
const { UserTypeDef } = require('./graphql/user/UserTypeDef');
const { UserResolver } = require('./graphql/user/UserResolvers');
const PORT = 4000;
const app = express();
const server = /* Error is thrown here */ new ApolloServer({
typeDefs: [
UserTypeDef,
],
resolvers: [
UserResolver,
]
});
server.applyMiddleware({ app });
app.listen({ port: PORT }, () => console.log(`? Server ready at http://localhost:4000${ server.graphqlPath }`),
);
这是UserTypeDef.js
const {gql} = require('apollo-server-express');
module.exports = {
UserTypeDef: gql`
type User {
id: ID
joinedDate: Int
name: String
}
`,
};
这是UserResolvers.js
module.exports = {
UserResolver: {
User: () => ({
id: 1,
joinedDate: 123123,
name: 'Test',
}),
},
};
这是我的依赖项,以防它们有用
"dependencies": {
"apollo-server-express": "^2.18.2"
"express": "^4.17.1",
"graphql": "^15.3.0",
"mongoose": "^5.10.4"
},
我尝试搜索,但是好像我以错误的方式导入或导出typeDefs和解析器
答案 0 :(得分:0)
好的,我这样修复了它:
index.js
const app = require('express')();
const { ApolloServer } = require('apollo-server-express');
const { typeDefs } = require('./graphql/typeDefs');
const { UserResolver } = require('./graphql/user/UserResolvers');
const server = new ApolloServer({
typeDefs,
resolvers: [ UserResolver ],
});
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () => console.log(`? Server ready at http://localhost:4000${ server.graphqlPath }`));
typeDefs.js
const { gql } = require('apollo-server-express');
const { UserTypeDef } = require('./user/UserTypeDef');
module.exports.typeDefs = gql`
${ UserTypeDef }
type Query {
users: [User]
}
`;
UserTypeDefs.js
const { gql } = require('apollo-server-express');
module.exports.UserTypeDef = gql`
type User {
id: ID
joinedDate: Int
name: String
}
`;
有一些有用的信息here