2017-08-07 28 views
1

我已經使用一個js基於graphql服務器sequelize定義了以下表(縮短爲了簡潔,並省略了一些其他實體)Graphql定製加入

const MetaData = sequelize.define(
    "MetaData", 
    { 
    MetaID: { 
     type: Sequelize.INTEGER, 
     autoIncrement: true, 
     primaryKey: true 
    }, 
    EntityID: { 
     type: Sequelize.INTEGER 
    }, 
    EntityType: { 
     type: Sequelize.STRING 
    }, 
    MetaKey: { 
     type: Sequelize.STRING 
    }, 
    MetaValue: { 
     type: Sequelize.STRING 
    } 
    } 
); 
const Staff = sequelize.define(
    "staff", 
    { 
    staffID: { 
     type: Sequelize.INTEGER, 
     autoIncrement: true, 
     primaryKey: true 
    }, 
    email: { 
     type: Sequelize.STRING 
    }, 
    firstName: { 
     type: Sequelize.STRING 
    }, 
    lastName: { 
     type: Sequelize.STRING 
    } 
    } 
); 
const Person = sequelize.define(
    "Person", 
    { 
    personID: { 
     type: Sequelize.INTEGER, 
     autoIncrement: true, 
     primaryKey: true 
    }, 
    firstName: Sequelize.STRING, 
    lastName: Sequelize.STRING 
    } 
); 

該元數據表的樣品是

[{ 
EntityID: 5, 
EntityType: "Staff", 
MetaKey: "Active", 
MetaValue: "true" 
},{ 
EntityID: 5, 
EntityType: "Person", 
MetaKey: "HairColor", 
MetaValue: "Blue" 
}] 

有沒有辦法,在graphqlsequelize,創建一個查詢,將返回與請求類型相關聯的元數據? 自定義解析器能夠解決這個解決方案而不需要額外調用額外的電話嗎?

例如,當我請求職員時,我希望能夠接收所有關聯的元數據。

編輯: 每the sequelize docs on polymorphic associations添加以下但不返回數據

Staff.MetaData = Staff.hasMany(MetaData, { 
    foreignKey: 'EntityID', 
    constraints: 'false', 
    scope: { 
     EntityType: 'Staff' 
    } 
}); 
MetaData.belongsTo(Staff, { 
    foreignKey: 'EntityID', 
    constraints: false, 
    as: 'Staff' 
}); 

MetaData.getItem = function() { 
    return this['get' + this.get('EntityType')](); 
}; 

回答

0

原來我錯過了我的resolvers.js通話

Staff: { 
    MetaData: resolver(Staff.MetaData) 
}