mickhansen / graphql-sequelize

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

Can't use Sequelize Operators in WHERE clauses (like, ne, contains...) #628

Closed a-zog closed 4 years ago

a-zog commented 5 years ago

I wasn't able to use queries having Sequelize Operators in where clauses.

This is a quick preview of the test:

capture d ecran 2018-09-21 a 12 33 09

Any idea about how to solve the issue?

mickhansen commented 5 years ago

What version of Sequelize? But in general i would recommend not using anapproach like this

a-zog commented 5 years ago

Version 4.38.1 What approach would you recommend?

Aukhan commented 5 years ago

@a-zog we'll have to request more detail or test with latest version for this issue, we have been using sequelize ops in the where clause and haven't found an issue, are you sure you wanted to query name and not fname here ?

@mickhansen please give us a few words on a better approach, I'm also curious about that, Thanks.

mickhansen commented 5 years ago

To me, exposing something like where taking an arbitrary sql-ish statement seems non-idiomatic with GraphQL. I'd expose expressive arguments that does what you need and then map those arguments to where statements. It's on my to do list to remove the built-in where support, and someone offer it as a plugin instead.

brianschardt commented 5 years ago

What if we want to do a special filter based on permissions, where we would need a sequleize or operator. Not sure why sequelize-graphql doesn't work with sequelize operators. It makes wonder about the future of this plugin.

mickhansen commented 5 years ago

@brianschardt define the filter as an argument and use $or based on it being present, or am i misunderstanding something?

As i mentioned i don't believe this to be idiomatic or good graphql schema design, but anyone is free to provide a plugin that offers this functionality - as the sole maintainer i don't want to spend resources maintaining it however.

vincentdesmares commented 5 years ago

@brianschardt you can use the operators without the $.

where: {
   or: [{id: 1}, {name: "foo"}]
}

But in general, I side with @mickhansen, it's better to add this behavior server side and use a flag to enable/disable it. Even more if it's related to security/permissions! For that, inject a where inside the "findOptions" parameter using the "before" hook.

Check https://github.com/mickhansen/graphql-sequelize/blob/c553924d1f964e6252048dae813da36356be1fb7/src/replaceWhereOperators.js

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.