簡體   English   中英

為什么在執行更改時會收到“無法為不可為空的字段返回 null”錯誤?

[英]Why am I getting a “Cannot return null for non-nullable field” error when doing a mutation?

我正在服務器端嘗試 (Apollo) GraphQL,並且遇到了一個可能很愚蠢的問題。 我正在嘗試從 faundb 數據庫中刪除 todo,但不斷收到以下鏈接圖像中顯示的錯誤。 問題是什么? 添加 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();
      }
    },
  }
}`

FQL Delete函數的結果與 FQL Get函數的結果相同。 它們都返回一個完整的 Document (請參閱關於Documents 的文檔)。

這是一個示例文檔:

Get(Ref(Collection("todos"), "302378028285035014"))

// result:

{
  ref: Ref(Collection("todo"), "302378028285035014"),
  ts: 1624629009620000,
  data: {
    task: "read some stuff on StackOverflow",
    status: false
  }
}

當您將Delete的結果返回給 GraphQL 解析器時,您發送了整個 Document,它實際上與架構不匹配。 你如何映射,以你的結果類似todos查詢,則需要添加一個待辦事項或刪除待辦事項這樣做。

此外,您似乎將ts字段作為文檔的 ID 返回給用戶。 ts字段是在每次寫入該文檔時更新的時間戳。

你可以做的一件事是使用ref.id字段來獲取 Reference 的 id 部分——這在 GraphQL 中更容易傳遞,但它確實需要你在想要使用它時重新創建完整的 Reference對於其他事情(如更新)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM