我正在尝试使用Apollo服务器从数据库中获取一些数据, 查询将如下所示
我将在users表中获取所有用户的列表,并使用users id查询TCourse表以获取CourseId,然后将使用CourseId查询Course表以获取标题
我尝试过,但得到了回报
{
"data": {
"userty": [
{
"tcourses": []
},
{
"tcourses": []
}
]
}
}
这是我的用户解析器代码
export default {
Query: {
userty : combineResolvers(
isAdmin,
async (parent, { where, offset }, { models }) => {
console.log(where)
return await models.User.findAll({
where: {role: where}, order: [['id', 'DESC']], limit: config.loadteacherLimit, offset: offset
})
}
),
},
User: {
tcourses: async (user, args, { models }) => {
const theTco = await models.TCourses.findAll({
where: {
UserId: user.id
}
})
const theCou = await models.Course.findAll({
where: {
id: theTco.CourseId
}
})
return theCou
},
},
}
这是我的用户架构代码
export default gql`
extend type Query {
userty(where: String, offset: Int): [User!]
}
type User {
id: ID!
fullname: String!
tcourses: [TCourses]
}
`;
这是我的TCourse模式代码
export default gql`
extend type Query {
tcourses: [TCourses]
}
type TCourses{
id:ID!
users:[User]
course:[Course]
}
`;
这是我的课程模式代码
export default gql`
extend type Query {
course: [Course]
}
type Course{
id: ID!
Title: String
}
`;
这是我的课程模型代码
const tcourses = (sequelize, DataTypes) => {
const TCourses = sequelize.define('tcourses', {
}, {
timestamps: false
});
TCourses.associate = models => {
TCourses.belongsTo(models.Course,{ as: 'Course'})
TCourses.belongsTo(models.User,{ as: 'User'})
};
return TCourses;
};
export default tcourses;
这是我的课程模型代码
const course = (sequelize, DataTypes) => {
const Course = sequelize.define('course', {
Title: {
type: DataTypes.STRING,
validate: {
notEmpty: {
args: true,
msg: 'Enter Course Title'
}
},
},
}, {
timestamps: false
})
Course.associate = models => {
Course.belongsTo(models.User, { as: 'Owners'})
Course.hasMany(models.TCourses,{ foreignKey: 'CourseId'})
};
return Course
};
export default course
用户是我的用户型号代码
const user = (sequelize, DataTypes) => {
const User = sequelize.define('user', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
unique: true,
primaryKey: true,
field: 'id'
},
fullname: DataTypes.STRING,
});
User.associate = models => {
User.hasMany(models.Course,{ foreignKey: 'OwnersId'})
User.hasMany(models.TCourses,{ foreignKey: 'UserId'})
};
return User;
};
export default user;
如何解决此问题?我在做什么错了?