Search code examples
graphqlrelayjsgraphql-jsrelaymodern

don't know why my cursorForObjectInConnection returns null?


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?


Solution

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