我已经成功地实现了一个非常简单的例子来使用graphql、apollo服务器和json服务器。但是delete方法返回空数据。我控制台.log(res.data(并且为空。有什么帮助吗?
解析程序突变方法:
type Mutation {
addCustomer(name: String!, email: String!, age: Int!): Customer!
editCustomer(id: ID!, name: String!, email: String!, age: Int!): Customer!
deleteCustomer(id: ID!): Customer!
}
已删除客户的类型Def:
deleteCustomer: async (_, { id }) => {
const res = await axios.delete("http://localhost:3000/customers/" + id);
return res.data;
},
如果提供的代码不够,代码就在这里:https://github.com/motapinto/learn-graphql-apollo-server
刚刚在那里运行了您的代码。看起来,如果删除它,它将返回null或空对象。在这种情况下,我认为您应该返回id,并检查res
变量,以确保没有错误,并将其与id一起返回。
类似下面的代码。但是,还有更好的办法。
deleteCustomer: async (_, { id }) => {
const res = await axios.delete("http://localhost:3000/customers/" + id);
if (res.status !== 200) return { success: false};
return { success: true, id};
};