简体   繁体   English

为什么在执行更改时会收到“无法为不可为空的字段返回 null”错误?

[英]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.

相关问题 为什么 graphql 返回“不能返回 null of non-nullable field mutation.createAccnt” - why is graphql returning “cannot return null of non-nullable field mutation.createAccnt” GraphQL 错误:无法为不可为空的字段 Mutation.deleteComment 返回 null - GraphQL Error: Cannot return null for non-nullable field Mutation.deleteComment 在 graphql 中更新/编辑用户数据(不能为不可空字段 Mutation.updateUser 返回 null) - Updating/editing user data in graphql(Cannot return null for non-nullable field Mutation.updateUser) 在添加新架构字段后,aws cdk nextjs graphql 突变无法为不可空类型返回 null - aws cdk nextjs graphql mutation cannot return null for non-nullable type after adding in new schema field 无法为 GraphQL 查询中的不可空字段返回 null - Cannot return null for non-nullable field in a GraphQL query 为什么这会抛出“不能为不可为空的字段 Query.hello 返回 null。”? - Why does this throw "Cannot return null for non-nullable field Query.hello."? GraphQL 查询返回错误“不能为不可为空的字段返回空值” - GraphQL query returns error "Cannot return null for non-nullable field" graphql 解析器返回 无法从 nodejs 中的异步 function 为非空字段返回 null - graphql resolver return Cannot return null for non-nullable field from asynchronous function in nodejs Prisma API返回关系但客户端返回“不能为非可空字段返回null”。 - Prisma API returns relation but client returns “cannot return null for non-nullable field..” 让TS抱怨与非空字段的空比较? - Getting TS to complain about null comparison to a non-nullable field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM