简体   繁体   中英

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. I'm trying to delete todo from faundb database, but keep getting the error shown in the linked image below. What is the problem? adding todo working properly but when I pass id to delete todo getting error attached in image

在此处输入图片说明

` **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 . They both return a full Document (See docs about 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. 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.

Also, it appears that you are returning the ts field to users as the Documents's ID. The ts field is a timestamp that is updated on every write to that Document.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM