删除突变 returns 空
delete mutation returns null
我有一个待办事项列表,我已经在其中成功地添加了新项目和更新缓存,现在正致力于添加删除突变。
我的最终目标是 return 从 delete 突变中获得一个 id,这个 id 可以用来更新客户端的整个待办事项列表。
我尝试将项目的 ID 传递给突变,但它 return 为空。
架构
type Todo {
_id: ID!
todo: String!
}
type RootMutation {
deleteTodo(_id: ID!): Todo
}
解析器
deleteTodo: async function({ _id }, req) {
return await Todo.deleteOne({ _id });
}
使用以下代码在 http://localhost:3000/graphql 的 graphql 界面中测试了此突变:
mutation {
deleteTodo (_id: "example0101010101001010101aasdsadasd"){
_id
}
}
收到此错误
{
"errors": [
{
"message": "Cannot return null for non-nullable field Todo._id.",
"locations": [
{
"line": 3,
"column": 3
}
],
"path": [
"deleteTodo",
"_id"
]
}
],
"data": {
"deleteTodo": null
}
}
当我将一个项目的 id 传递给突变时,它应该 return 这个已删除项目的 id 但我一直得到 null。
我怀疑我的解析器功能可能有问题。
Link到githubrepo
您可能不想在这里使用 deleteOne
方法。而是查看 type definitions for mongoose
, remove
, deleteOne
and deleteMany
do not return the deleted document. Use findByIdAndRemove
,这将 return 已删除的文档,如果未找到则为 null。
我有一个待办事项列表,我已经在其中成功地添加了新项目和更新缓存,现在正致力于添加删除突变。 我的最终目标是 return 从 delete 突变中获得一个 id,这个 id 可以用来更新客户端的整个待办事项列表。
我尝试将项目的 ID 传递给突变,但它 return 为空。
架构
type Todo {
_id: ID!
todo: String!
}
type RootMutation {
deleteTodo(_id: ID!): Todo
}
解析器
deleteTodo: async function({ _id }, req) {
return await Todo.deleteOne({ _id });
}
使用以下代码在 http://localhost:3000/graphql 的 graphql 界面中测试了此突变:
mutation {
deleteTodo (_id: "example0101010101001010101aasdsadasd"){
_id
}
}
收到此错误
{
"errors": [
{
"message": "Cannot return null for non-nullable field Todo._id.",
"locations": [
{
"line": 3,
"column": 3
}
],
"path": [
"deleteTodo",
"_id"
]
}
],
"data": {
"deleteTodo": null
}
}
当我将一个项目的 id 传递给突变时,它应该 return 这个已删除项目的 id 但我一直得到 null。 我怀疑我的解析器功能可能有问题。
Link到githubrepo
您可能不想在这里使用 deleteOne
方法。而是查看 type definitions for mongoose
, remove
, deleteOne
and deleteMany
do not return the deleted document. Use findByIdAndRemove
,这将 return 已删除的文档,如果未找到则为 null。