Я работаю с Sequelize на сервере Apollo-Server. До сих пор это было просто, но теперь у меня возникла небольшая проблема с рефакторингом моего кода. В частности, у меня проблемы с заполнением определенных полей.
Глядя на фрагменты кода, которые я включил, первая и вторая версии запроса comment
работают должным образом. Однако третий и четвертый результат приводят к ошибкам Error: missing FROM-clause entry for table \"votes->votedBy\"
и Error: Include unexpected. Element has to be either a Model, an Association or an object.
соответственно.
Я должен заполнить поле User
несколько раз (addedBy, commentedBy, votedBy
). То же самое относится и к голосованию, поэтому вместо того, чтобы многократно печатать все их логики c, я решил сохранить их в соответствующих переменных, как показано ниже. Я заметил, что если я использую только один экземпляр хранимых полей (commentVersionTwo
), это работает. Любое дополнительное использование переменных полей (commentVersionThree
и commentVersionFour
) и его разрывы. Что мне здесь не хватает?
Поля пользователей и голосов
import models from '../models';
const { Profile, User, Vote } = models;
export const userFields = {
model: User,
include: {
model: Profile,
as: 'profile',
},
};
export const voteFields = {
model: Vote,
as: 'votes',
include: {
as: 'votedBy',
...userFields,
},
};
Запрос комментариев
import { userFields, voteFields } from '../populate-fields';
export default {
Query: {
commentVersionOne: (
parent,
{ commentId },
{ models: { Comment, Vote, User, Profile } }
) => {
return Comment.findByPk(commentId, {
include: [
{
as: 'commentedBy',
model: User,
include: {
model: Profile,
as: 'profile',
}
},
{
model: Vote,
as: 'votes',
include: {
as: 'votedBy',
model: User,
include: {
model: Profile,
as: 'profile'
}
}
},
],
})
.then((foundComment) => foundComment.get({ plain: true }))
.catch((error) => {
throw new Error(error.message);
});
},
commentVersionTwo: (
parent,
{ commentId },
{ models: { Comment, Vote, User, Profile } }
) => {
return Comment.findByPk(commentId, {
include: [
{
as: 'commentedBy',
...userFields
},
{
model: Vote,
as: 'votes',
include: {
as: 'votedBy',
model: User,
include: {
model: Profile,
as: 'profile'
}
}
},
],
})
.then((foundComment) => foundComment.get({ plain: true }))
.catch((error) => {
throw new Error(error.message);
});
},
// Error: missing FROM-clause entry for table \"votes->votedBy\"
commentVersionThree: (
parent,
{ commentId },
{ models: { Comment, Vote, User, Profile } }
) => {
return Comment.findByPk(commentId, {
include: [
{
as: 'commentedBy',
...userFields
},
{
model: Vote,
as: 'votes',
include: {
as: 'votedBy',
...userFields
}
},
],
})
.then((foundComment) => foundComment.get({ plain: true }))
.catch((error) => {
throw new Error(error.message);
});
},
// Error: Include unexpected. Element has to be either a Model, an Association or an object.
commentVersionFour: (
parent,
{ commentId },
{ models: { Comment } }
) => {
return Comment.findByPk(commentId, {
include: [
{
as: 'commentedBy',
...userFields
},
{...voteFields}
],
})
.then((foundComment) => foundComment.get({ plain: true }))
.catch((error) => {
throw new Error(error.message);
});
},
}
}