我正在使用graphql
,sequelize
和graphql-sequelize
, 在解决“IS A”关系时遇到了一些麻烦。
我的续集模型如下:
// models.js
// User table
let User = sequelize.define('user', {
id: Sequelize.INTEGER
name: Sequelize.STRING
});
// Patient table
let Patient = sequelize.define('patient', {
bloodType: Sequelize.STRING
});
// Defines "IS A" relationship: [Patient] IS A [User]
Patient.belongsTo(models.User, {
foreignKey: {
name: 'id',
type: DataTypes.INTEGER,
primaryKey: true
},
foreignKeyConstraint: true
})
// Doctor table
let Doctor = sequelize.define('doctor', {
registry: Sequelize.STRING
});
// Defines "IS A" relationship: [Doctor] IS A [User]
Doctor.belongsTo(models.User, {
foreignKey: {
name: 'id',
type: DataTypes.INTEGER,
primaryKey: true
},
foreignKeyConstraint: true
})
这是我的 graphql 架构:
// graphql.js
import resolver from 'graphql-sequelize';
import * as models from './models';
let userType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: new GraphQLNonNull(GraphQLInt) },
name: { type: GraphQLString }
}
});
let patientType = new GraphQLObjectType({
name: 'Patient',
fields: {
id: { type: new GraphQLNonNull(GraphQLInt) },
bloodType: { type: GraphQLString },
user: {
type: new GraphQLNonNull(userType),
// IMPORTANT!
// How can I call resolver if I don't have a assotiation property like Patient.User?
resolve: resolver()
}
}
});
// [doctorType omitted]
let schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
users: {
type: userType,
resolve: resolver(models.User)
},
patients: {
type: patientType,
resolve: resolver(models.Patient)
}
// [doctor field omitted]
}
})
});
resolver
在架构(users
和)的字段上调用该方法patients
可以正常工作,但我的问题是如何调用解析器patientType
以返回其用户,因为我没有关联属性。
谢谢。