1
votes

I am a newbie to GraphQL and trying to write an update mutation. However, I am receiving Resolve function for \"User.id\" returned undefined" error although the database is actually got updated.

What am I doing wrong?

userSchema.js:

import Sequelize from 'sequelize';
import SqlHelper from '../helpers/sqlhelper';

const config = require('../../config');

const sequelizer = new SqlHelper(config).Init();


const createUser = sequelizer.define(
  'createUser',
  {
    ...
    }
);
const updateUser = sequelizer.define(
  'updateUser',
  {
    id: {
      type: Sequelize.UUID,
      field: 'Id',
      primaryKey: true,
      defaultValue: Sequelize.UUIDV4,
    },
    username: {
      type: Sequelize.STRING,
      field: 'Username',
      allowNull: true,
    },    
    email: {
      type: Sequelize.STRING,
      field: 'Email',
      allowNull: true,
    },
    firstname: {
      type: Sequelize.STRING,
      field: 'FirstName',
      allowNull: true,
    },
    lastname: {
      type: Sequelize.STRING,
      field: 'LastName',
      allowNull: true,
    },
   ....
  },
  {
    // define the table's name
    tableName: 'Users',
  },
);

module.exports = User;

UserResolver.js:

import User from '../dbschemas/user';
import Sequelize from 'sequelize';
const Op = Sequelize.Op;

export default {
  
  Mutation: {
    createUser: async (obj, args) =>
      (await User.create(args)),
    updateUser: async (obj, args) =>
      (await User.update(args,
        {
          where: {
            id: args.id,
          },
          returning: true
        }))

  }
};

Although calling updateUser from GraphiQL updates the records (in db), it results in a "Resolve function for \"User.id\" returned undefined" error:

mutation{
  updateUser(id: "2ecd38ca-cf12-4e79-ac93-e922f24af9e3", 
    username: "newUserTesting",  
    email: "[email protected]",     
    lastname: "TestUserLName", 
    firstname: "fname1") {
   id
  } 
}

{
  "data": null,
  "errors": [
    {
      "message": "Resolve function for \"User.id\" returned undefined",
      "locations": [
        {
          "line": 16,
          "column": 4
        }
      ],
      "path": [
        "updateUser",
        "id"
      ]
    }
  ]
}
2
What happens if you return another field from a mutation other than id? Perhaps try firstname instead.Andrija Ćeranić
same error: "Resolve function for \"User.firstname\" returned undefined"user3520042

2 Answers

1
votes

The issue is clear, your resolver does not return an object containing id. The docs say that Model.update returns an array in which the 2nd element is the affected row. Hence, your resolver should look like:

async updateUser(obj, args) {
  const resultArray = await User.update( ... )
  return resultArray[1]
}

... To be replaced by whatever you need.

1
votes

So apparently, update does NOT return affected rows for MSSQL, only the number of records affected.

This is true only for postgres when returning: true:

public static update(values: Object, options: Object): Promise<Array<affectedCount, affectedRows>>

Setting returning: true (for MSSQL) returns undefined (and order of params in the array is not even in the right order... i.e. first affectedRows -> undefined, then affectedCount ->num of affected rows.)

Tho get an object back you would need to do something like this:

Mutation: {
createUser: async (obj, args) =>
  (await User.create(args.user)),
updateUser: async (obj, args, context, info) =>{        
  let user = args.user;
  let response = await User.update(user,
      {
        where: {
          [Op.or]: [{ email: user.email }, { id: user.id }, { username: user.username }, { lastname: user.lastname}]
        },
        //returning: true //not working... only for postgres db :(
      }).then(ret => {   
        console.log('ret', ret);         
        return ret[0];
      }).catch(error => {
        console.log('error', error)
      });

  if (response > 0) return user; //return record 

  //return response > 0; //return true
}      

}