Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graphql 中继节点定义抛出“;'的右侧;instanceof';“不可赎回”;_Graphql_Instanceof_Relayjs - Fatal编程技术网

Graphql 中继节点定义抛出“;'的右侧;instanceof';“不可赎回”;

Graphql 中继节点定义抛出“;'的右侧;instanceof';“不可赎回”;,graphql,instanceof,relayjs,Graphql,Instanceof,Relayjs,我正在学习GraphQL,在过去的几天里,我刚刚开始在我的节点服务器上实现GraphQL中继。在声明中继节点定义时,我遇到错误“instanceof的右侧不可调用”,其中右侧是具有构造函数的对象。据我所知,这不是由于使用不当,也考虑到这是从文件中复制的。我不确定当它正常工作时预期的结果是什么,我假设它返回要进行工作的GraphQL类型,并返回请求的数据 var {nodeInterface, nodeField} = nodeDefinitions( (globalId) => {

我正在学习GraphQL,在过去的几天里,我刚刚开始在我的节点服务器上实现GraphQL中继。在声明中继节点定义时,我遇到错误“instanceof的右侧不可调用”,其中右侧是具有构造函数的对象。据我所知,这不是由于使用不当,也考虑到这是从文件中复制的。我不确定当它正常工作时预期的结果是什么,我假设它返回要进行工作的GraphQL类型,并返回请求的数据

var {nodeInterface, nodeField} = nodeDefinitions(
  (globalId) => {
    var {type, id} = fromGlobalId(globalId);
     console.log(type);
    if (type === 'User') {
      return db.models.user.findById(id)
   } else if (type === 'Video') {
       return db.models.video.findById(id)
    }
    else if (type === 'Producer') {
      return db.models.user.findById(id)
    }
    else if (type === 'Viewer') {
      return db.models.user.findById(id)
    }else {
      return null;
    }
  },
  (obj) => {

  console.log(obj);               // Sequelize object
  console.log(User);              // user
  console.log(User.constructor);  // valid constructor

  // This is where the error occurs
  if (obj instanceof User) {
    return UserType;
  } else if (obj instanceof Video)  {
    return VideoType;
  } else {
    return null;
  }
});
注:

  • 使用Sequelize ORM
  • 用户是GraphQL中的一个接口,该模式由查看器、生产者和GeneralUser类型实现。另一方面,我的psql数据库有一个用户表,这就是为什么第二个函数只检查用户,而不检查这些附加类型
  • 我对用户、视频等的所有其他查询都可以正常工作,只有在节点和globalId中断时才进行搜索
无法调用“具有构造函数的对象”。可能需要将该对象设置为具有以下构造函数的类:

class User {
  constructor(id, name, email) {
    this.id = id;
    // etc
  }
}

您需要从以下位置更改代码:

...

if (obj instanceof User) {

...

} else if (obj instanceof Video)  {

....
致:


它有一个构造函数,我正在把它记录到我的控制台上。我已经找到了一个在未来应该是稳定的工作环境。
...

if (obj instanceof User.Instance) {

...

} else if (obj instanceof Video.Instance)  {

....