mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

Ordering by the join (association, 1-many include) #586

Closed moseleyi closed 6 years ago

moseleyi commented 6 years ago

I think I saw somewhere how to use the following syntax: $table.column$ but I can't find it, hopefully you guys can refresh my memory.

I have main table called Organizations, in before function I'm including hasMany model called organizationPeople that is a junction table, I used separate : true, to run it in separate query. Next theres another include to Users module (to get the person's name). Now I would like to order the results of that query by a column from Users.

This is a mock up of my current before:

options.include = [{
    model : getModel("People"),
    separate : true,
    order : [
        ["last_logged_in", "DESC NULLS LAST"]
    ],
    include : [{
        model : getModel("Users"),
        required : true
    }]
]};

The last_logged_in column is on Users table, how can I reference it?

mickhansen commented 6 years ago

I don't know that include supports order? In GraphQL, unless you always include that model, you generally want to use a subquery for ordering. Otherwise, include ordering is something like (on the main query part) order: [[People, 'last_logged_in', 'DESC NULLS LAST']]

intellix commented 6 years ago

@moseleyi I think you can achieve it like this: https://github.com/mickhansen/graphql-sequelize/issues/573#issuecomment-373812402