join-monster
join-monster copied to clipboard
many-to-many bug!!!
- I want find a user followers and his followees, so here we go:
const User = new GraphQLObjectType({
//...
fields: {
followee: {
description: 'Users followee',
type: new GraphQLList(User),
// many-to-many is supported too, via an intermediate join table
junction: {
sqlTable: 'relationships',
uniqueKey: ['follower_id','followee_id'],
include: {
closeness: {
sqlColumn: 'closeness'
}
},
sqlJoins: [
(followeeTable, relationTable) => `${followeeTable}.id = ${relationTable}.follower_id`,
(relationTable, followerTable) => `${relationTable}.followee_id = ${followerTable}.id`
]
}
},
follower: {
description: 'Users follwer',
type: new GraphQLList(User),
// many-to-many is supported too, via an intermediate join table
junction: {
sqlTable: 'relationships',
uniqueKey: ['follower_id','followee_id'],
include: {
closeness: {
sqlColumn: 'closeness'
}
},
sqlJoins: [
(followerTable, relationTable) => `${followerTable}.id = ${relationTable}.follower_id`,
(relationTable, followeeTable) => `${relationTable}.followee_id = ${followeeTable}.id`
]
}
},
}
});
than i query
{
user(id:1){
follower{
id
}
followee{
id
}
}
}
But i got the same result,the user follower and his followee are same, but it doesn't in database.
Doesn't look like a bug to me. The problem is that the sqlJoins for both fields are the same, except for the parameter name. But effectively they are the same functions. You need to move the column names around.
yeah, I need to move the column names around. can you give me a correct code snippet.
sqlJoins: [
(followeeTable, relationTable) => `${followeeTable}.id = ${relationTable}.follower_id`,
(relationTable, followerTable) => `${relationTable}.followee_id = ${followerTable}.id`
]
should be
sqlJoins: [
(followeeTable, relationTable) => `${followeeTable}.id = ${relationTable}.followee_id`,
(relationTable, followerTable) => `${relationTable}.follower_id = ${followerTable}.id`
]
Let us know if this works out for you so we can close this.