0

I am having this error on response:

Cannot return null for non-nullable field TodoEdge.cursor.

This is my mutation code:

  mutateAndGetPayload: async ({text}, context) => {
      let newTodo = new TodoModel({ text, _creatorUserId: context.user._id });
      await newTodo.save(); 
      return {newTodo}; 
  },
  outputFields: {
    todoEdge: {
      type: GraphQLTodoEdge,
      resolve: async ({newTodo}, args,context) => {
        const todos = await getTodosByUserContext(context.user._id, 'any');
        console.log("cursorForObjectInConnection = ",cursorForObjectInConnection(todos, newTodo)) // this logs null?
        return {
          cursor: cursorForObjectInConnection(todos, newTodo),
          node: newTodo,
        };
      },
    },

todos and newTodo is retreived from mongoose database. I think I am following this relevant todo-modern sample properly. Help?

gpbaculio
  • 5,693
  • 13
  • 60
  • 102

1 Answers1

3

Let's try following:

1) Get todos from your DB like

const todos = await Todo.find({});

You can get todos for a specific user as per your Todo Schema. Todo.find({ userid: context.user._id});

2) once you get your todos then get cursor:

const cursor = offsetToCursor(todos.length);

This worked for me. Give it a try.

sushil bansal
  • 1,894
  • 2
  • 12
  • 13