我正在学习GraphQL,并且在过去几天刚刚开始在我的节点服务器上实现graphql-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;
}
});
笔记:
-
使用Sequelize ORM .
-
User是GraphQL中的接口,由Viewer,Producer和GeneralUser类型实现 . 另一方面,我的psql数据库有一个User表,这就是为什么第二个函数只检查User而不是这些其他类型的原因 .
-
我对用户,视频等的所有其他查询都运行正常,只有在按节点&& globalId进行搜索时才会出现问题
2 回答
“具有构造函数的对象”不可调用 . 可能你需要使用这样的构造函数使该对象成为一个类:
您需要更改以下代码:
至: