【发布时间】:2018-11-17 11:28:41
【问题描述】:
我一直在寻找这个错误,但没有找到成功的解决方案。我正在学习graphql,所以我构建了一个小应用程序,这是我的package.json:
"dependencies": {
"apollo-server-express": "^1.3.6",
"babel-watch": "^2.0.7",
"body-parser": "^1.18.3",
"express": "^4.16.3",
"graphql": "^0.13.2",
"graphql-tools": "^3.0.2",
"mongoose": "^5.1.3",
"node-uuid": "^1.4.8"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0"
}
我的 schema.js:
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'
import resolvers from './resolvers'
const typeDefs = `
type Author {
id: String
name: String
age: Int
books: [String]
}
type Query {
authors: [Author]
author(id: String): Author
}
type Mutation {
addAuthor(name: String!, age: Int!, books: [String]!):Author
}
`
const schema = makeExecutableSchema({typeDefs, resolvers});
export default schema;
我的 resolvers.js:
import mongoose from 'mongoose';
import authorModel from './model/author';
const resolvers = {
Query: {
authors: () => {
//return fakeData;
},
// root is never used, and args holds our filter params
author: (root, args) => {
//const id = args.id;
//return fakeData.find((author) => author.id === id);
}
},
// mutations changes the data states
Mutation: {
addAuthor: (root, {name, age, books}) => {
console.log(name, age, books);
const author = new authorModel({name: name, age: age, books: books});
return author.save();
}
}
};
export default resolvers;
最后,我的模型 author.js:
import mongoose from 'mongoose';
// to generate our unique IDs
import uuid from 'node-uuid';
const schema = mongoose.Schema;
// this schema must be compatible with schema from graphQl
const authorSchema = new schema({
id: {type: String, default: uuid.v1},
name: String,
age: Number,
books: [String]
});
export default authorSchema;
一切正常,我的 express 与 apollo-server-express 正常工作,当我使用 addAuthor 突变时,解析器中的函数 addAuthor 被调用,但是在创建作者 objetc 模型时出错,错误:
TypeError: _author2.default is not a constructor
at addAuthor (/home/desinv04/estudos/graphql/worldcup-graphql/resolvers.js:46:28)
at resolveFieldValueOrError (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:531:18)
at resolveField (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:495:16)
at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:339:18
at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/jsutils/promiseReduce.js:25:10
at Array.reduce (<anonymous>)
at promiseReduce (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/jsutils/promiseReduce.js:22:17)
at executeFieldsSerially (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:336:38)
at executeOperation (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:289:55)
at executeImpl (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:154:14)
at Object.execute (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:131:229)
at doRunQuery (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/apollo-server-core/src/runQuery.ts:194:7)
at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/apollo-server-core/src/runQuery.ts:79:39
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
我不明白为什么会出现这个错误,提前谢谢。
【问题讨论】:
-
我认为在猫鼬中,模式不是模型。您必须先创建模型:
export default mongoose.model(authorSchema)。请参阅文档:mongoosejs.com/docs/populate.html
标签: express graphql apollo-server