join-monster icon indicating copy to clipboard operation
join-monster copied to clipboard

many-to-many bug!!!

Open fangjj opened this issue 8 years ago • 3 comments

  1. 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.

fangjj avatar Sep 02 '17 03:09 fangjj

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.

acarl005 avatar Sep 06 '17 05:09 acarl005

yeah, I need to move the column names around. can you give me a correct code snippet.

fangjj avatar Sep 06 '17 05:09 fangjj

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.

GlennMatthys avatar Nov 02 '18 13:11 GlennMatthys