错误:解析器中定义了“突变”,架构中未定义

时间:2019-08-15 00:32:54

标签: node.js mongodb graphql react-apollo apollo-server

我正在学习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;

1 个答案:

答案 0 :(得分:0)

如错误所示,您的架构中没有名为Mutations的类型。它的名称为Mutation。解析程序映射中的任何类型和/或字段都必须与您的架构完全匹配。