Ajar
Ajar

Reputation: 1051

How to add query logic to graphql-sequelize based setup?

Here is a simple scenario
I want to query GraphQL and fetch all the students that have attended school more then 200 days this year.
I have a Student sequelize model, a matching GraphQL Object, a students Query which accepts the days int as an argument { students(days:200) }
What I'm obviously trying to do is to implement the logic "get all students where school_visits counter is greater than 200" just as an example...
I'm using graphql-sequelize to glue between sequelize and GraphQL.
a simple example query definition looks like this:

students: {
                type: new GraphQLList(Student),
                args:{
                    id  : {type:GraphQLInt}, 
                    days: {type:GraphQLInt}
                },
                resolve: resolver(models.Student)
            }

as you can notice, this structure doesn't allow me to insert some logic to the query... If you had any experience working with GraphQl and Sequelize - this is a very generic situation... I'd appreciate any help resolving it.
Cheers
Ajar

Upvotes: 2

Views: 1915

Answers (1)

Vlady Veselinov
Vlady Veselinov

Reputation: 5401

Looking at the Sequelize docs, you can try doing something like this in the students resolve function:

resolve(root, args) {
    // Assuming you've imported your model
    return Db.models.student.findAll({
        where: {
            days: {
                // greater than
                $gt: args.days
            }
        }
    });
}

Upvotes: 4

Related Questions