2017-04-03 46 views
0

我正在學習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當它打破

回答

0

的「使用構造函數對象」是不可調用搜索時。也許你需要做的是對象的類,像這樣一個構造函數:

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

它有一個構造函數,我將它記錄到我的控制檯。我已經找到了一個應該穩定的未來工作。 –

0

您需要的代碼更改:

... 

if (obj instanceof User) { 

... 

} else if (obj instanceof Video) { 

.... 

到:

... 

if (obj instanceof User.Instance) { 

... 

} else if (obj instanceof Video.Instance) { 

.... 
相關問題