简体   繁体   English

GraphQL错误更新变异“ \\” User.id \\”的解析函数返回未定义”

[英]GraphQL error update mutation “Resolve function for \”User.id\“ returned undefined”

I am a newbie to GraphQL and trying to write an update mutation. 我是GraphQL的新手,正在尝试编写更新突变。 However, I am receiving Resolve function for \\"User.id\\" returned undefined" error although the database is actually got updated. 但是,尽管实际上已更新数据库,但是我正在收到\\“ User.id \\”返回未定义“错误的解析函数。

What am I doing wrong? 我究竟做错了什么?

userSchema.js: 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: 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: 尽管从GraphiQL调用updateUser会更新记录(以db为单位),但会导致“为\\“ User.id \\”返回未定义的解析函数”错误:

 mutation{ updateUser(id: "2ecd38ca-cf12-4e79-ac93-e922f24af9e3", username: "newUserTesting", email: "testemail@yahoo.com", lastname: "TestUserLName", firstname: "fname1") { id } } 

 { "data": null, "errors": [ { "message": "Resolve function for \\"User.id\\" returned undefined", "locations": [ { "line": 16, "column": 4 } ], "path": [ "updateUser", "id" ] } ] } 

The issue is clear, your resolver does not return an object containing id . 问题很明显,您的解析器不会返回包含id的对象。 The docs say that Model.update returns an array in which the 2nd element is the affected row. 文档Model.update返回一个数组,其中第二个元素是受影响的行。 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. ...被您需要的东西所取代。

So apparently, update does NOT return affected rows for MSSQL, only the number of records affected. 因此,显然,更新不会为MSSQL返回受影响的行,而仅返回受影响的记录数。

This is true only for postgres when returning: true: 这仅适用于postgres返回时: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... ie first affectedRows -> undefined, then affectedCount ->num of affected rows.) 设置返回值:true(对于MSSQL)返回undefined(并且数组中的参数顺序甚至没有正确的顺序……即,首先是受影响的行->未定义,然后是受影响的计数->受影响的行数。)

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
}      

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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