我正在學習GraphQL,並在過去幾天剛開始在我的節點服務器上實現graphql-relay。當聲明我的中繼節點定義時,我收到錯誤「instanceof'的右手邊不可調用」,右手邊是帶有構造函數的對象。據我所知,這不是由於不當使用,也考慮到這是從文檔複製。我不確定當它正常工作時預期的結果是什麼,我假定它返回GraphQL類型以便通過工作並返回請求的數據。中繼節點定義拋出「instanceof'的右側不可調用」
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中的一個接口,該架構由Viewer,Producer和GeneralUser類型實現。另一方面,我的psql數據庫有一個用戶表,這就是爲什麼第二個函數只檢查用戶而不是這些附加類型。
- 我所有的其他查詢,爲用戶,視頻等做工精細,它只有通過節點& & globalId當它打破
它有一個構造函數,我將它記錄到我的控制檯。我已經找到了一個應該穩定的未來工作。 –