我正在学习graphQL。有人可以帮我理解为什么在架构中没有识别出突变吗?
遇到此错误:
Error: "Mutations" defined in resolvers, but not in schema
以下是代码:
模式:
const typeDefs = gql`
type Author {
age: String
name: String
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;
解析器:
const resolvers = {
Query: {
authors: () => {
// return authors;
},
author: (root, { id }) => {
// return authors.find(author => author.id === id);
}
},
Mutations: {
addAuthor: (root, {name, age, books}) => {
const author = new authorModel({name, age, books});
return author.save();
}
}
}
export default resolvers;
答案 0 :(得分:0)
如错误所示,您的架构中没有名为Mutations
的类型。它的名称为Mutation
。解析程序映射中的任何类型和/或字段都必须与您的架构完全匹配。