I'm trying to figure out how to make a query with Sequelize.js that outputs something like
SELECT ... WHERE C1 = X AND (C2 LIKE '%Y%' OR C3 LIKE '%Y%')... to MySQL
code
const resolvers = {
Query: {
findItems(_, args) {
return Item.findAll({
limit: 10,
active: 1,
offset: args.offset,
order: [['created_at', 'DESC']],
$or: [
{
column_a: {
$like: args.y,
},
},
{
column_b: {
$like: args.y,
},
}]
});
}
}
}
It's querying SELECT * FROM item ORDER BY created_at DESC LIMIT...
(no where)
Schema from GraphQL
const typeDefinitions = `
type Item {
column_a: String,
column_b: String,
active: Int,
}
type Query {
findItems(column_a: String, column_b: String, active: Int, offset: Int): [Item]
}
type Mutation {
//..
}
schema {
query: Query
mutation: Mutation
}`;
export default [typeDefinitions];
The docs from Sequelize.js don't explain how to do this query, I don't know if the problem is coming from the GraphQL query either.
Here's the query
{
findItems(column_a: "hello", column_b:"hello", active: 1, offset: 0) {
column_a
column_b
active
created_at
}
}
SELECT * FROM item ORDER BY created_at DESC LIMIT...
(no where)
You can always pass in an optional where object to filter the query. See Querying
const resolvers = {
Query: {
findItems(_, args) {
return Item.findAll({
where: {
column_a: args.x
},
limit: 10,
active: 1,
offset: args.offset,
order: [['created_at', 'DESC']],
$or: [
{
column_a: {
$like: args.y,
},
},
{
column_b: {
$like: args.y,
},
}]
});
}
}
}
Had to put the $or inside the where and add a dollar sign.
const resolvers = {
Query: {
findItems(_, args) {
return Item.findAll({
where: {
active: 1,
$or: [
{
column_a: {
$like: args.y,
},
},
{
column_b: {
$like: args.y,
},
}],
},
limit: 10,
active: 1,
offset: args.offset,
order: [['created_at', 'DESC']],
});
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.