中继节点定义抛出"Right-hand side of 'instanceof' is not callable"



我正在学习 GraphQL,并且在过去的几天里刚刚开始在我的节点服务器上实现 graphql-relay。在声明我的 Relay 节点定义时,我收到错误"'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;
}
});

笔记:

  • 使用续集ORM。
  • User 是 GraphQL 中的一个接口,由 Viewer、Producer 和 GeneralUser 类型实现的模式。另一方面,我的 psql 数据库有一个 User 表,这就是为什么第二个函数只检查 User 而不是这些附加类型。
  • 我对用户、视频等的所有其他查询都可以正常工作,只有在按节点 &&&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)  {
....

最新更新