Я искал эту ошибку, но не нашел успешного решения.Я изучаю 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;
Все работает нормально, мой экспресс работает нормально с apollo-server-express, и когда я использую мутацию addAuthor, функция addAuthorв resolver вызывается, но возникает ошибка при создании автора 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)
Не могу понять, почему возникает эта ошибка, заранее спасибо.