[英]Why am I getting a “Cannot return null for non-nullable field” error when doing a mutation?
I'm trying my hand at (Apollo) GraphQL on the server-side and have been having a probably silly issue.我正在服务器端尝试 (Apollo) GraphQL,并且遇到了一个可能很愚蠢的问题。 I'm trying to delete todo from faundb database, but keep getting the error shown in the linked image below.
我正在尝试从 faundb 数据库中删除 todo,但不断收到以下链接图像中显示的错误。 What is the problem?
问题是什么? adding todo working properly but when I pass id to delete todo getting error attached in image
添加 todo 工作正常,但是当我通过 id 删除 todo 时,图像中附加了错误
` **Schema**
const GET_TODOS = gql`
{
todos {
task,
id,
status
}
}
`;
const ADD_TODO = gql`
mutation addTodo($task: String!){
addTodo(task: $task){
task
}
}
`
const deleteTodo = gql`
mutation deleteTask($id: ID!) {
deleteTask(id: $id) {
task
}
}
`;
Mutation
const typeDefs = gql`
type Query {
todos: [Todo!]
}
type Mutation {
addTodo(task: String!): Todo
deleteTask(id: ID!): Todo
}
type Todo {
id: ID!
task: String!
status: Boolean!
}
`
const resolvers = {
Query: {
todos: async (root, args, context) => {
try {
var adminClient = new faunadb.Client({ secret: 'fnAD5rID0MACBTs47TwGR8D1Itfdj3cyo79VVDWD' });
const result = await adminClient.query(
q.Map(
q.Paginate(q.Match(q.Index('task'))),
q.Lambda(x => q.Get(x))
)
)
console.log(result.data)
return result.data.map(d=>{
return {
id: d.ts,
status: d.data.status,
task: d.data.task
}
})
}
catch (err) {
console.log(err)
return err.toString();
}
}
// authorByName: (root, args, context) => {
// console.log('hihhihi', args.name)
// return authors.find(x => x.name === args.name) || 'NOTFOUND'
// },
},
Mutation: {
addTodo: async (_, { task }) => {
try {
var adminClient = new faunadb.Client({ secret: 'fnAD5rID0MACBTs47TwGR8D1Itfdj3cyo79VVDWD' });
const result = await adminClient.query(
q.Create(
q.Collection('todos'),
{
data: {
task: task,
status: true
}
},
)
)
return result.ref.data;
}
catch (err) {
console.log(err)
}
},
deleteTask: async (_, { id }) => {
try {
console.log(id);
var adminClient = new faunadb.Client({ secret: 'fnAD5rID0MACBTs47TwGR8D1Itfdj3cyo79VVDWD' });
const result = await adminClient.query(
q.Delete(q.Ref(q.Collection("todos"), id))
);
console.log(result);
return result.ref.data;
} catch (error) {
return error.toString();
}
},
}
}`
The result from the FQL Delete
function is the same as the FQL Get
function . FQL
Delete
函数的结果与 FQL Get
函数的结果相同。 They both return a full Document (See docs about Documents ).它们都返回一个完整的 Document (请参阅关于Documents 的文档)。
Here is an example Document:这是一个示例文档:
Get(Ref(Collection("todos"), "302378028285035014"))
// result:
{
ref: Ref(Collection("todo"), "302378028285035014"),
ts: 1624629009620000,
data: {
task: "read some stuff on StackOverflow",
status: false
}
}
When you return the result of Delete
to the GraphQL resolver, you sent the whole Document, which doesn't actually match the schema.当您将
Delete
的结果返回给 GraphQL 解析器时,您发送了整个 Document,它实际上与架构不匹配。 Similar to how you mapped over the results for your todos
Query, you will need to do the same for adding a todo or deleting a todo.你如何映射,以你的结果类似
todos
查询,则需要添加一个待办事项或删除待办事项这样做。
Also, it appears that you are returning the ts
field to users as the Documents's ID.此外,您似乎将
ts
字段作为文档的 ID 返回给用户。 The ts
field is a timestamp that is updated on every write to that Document. ts
字段是在每次写入该文档时更新的时间戳。
One thing you can do is use the ref.id
field to obtain just the id part of the Reference -- that is easier to pass around in GraphQL, but it does require that you re-create the full Reference when you want to use it for other things (like Update).你可以做的一件事是使用
ref.id
字段来获取 Reference 的 id 部分——这在 GraphQL 中更容易传递,但它确实需要你在想要使用它时重新创建完整的 Reference对于其他事情(如更新)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.