2016-07-31 99 views
0

我想弄清楚如何使Sequelize.js查询输出类似于查询从GraphQL到Sequelize.js SELECT ... WHERE COL1 = X和(COL2像Y或COL3像Y)

SELECT ... WHERE C1 = X AND (C2 LIKE '%Y%' OR C3 LIKE '%Y%')... to MySQL

代码

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, 
      }, 
      }] 
     }); 
    } 
    } 
} 

它从GraphQL

012查询 SELECT * FROM item ORDER BY created_at DESC LIMIT...(任何地方)

模式

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]; 

来自Sequelize.js的文档没有解释如何执行此查询,我不知道该问题是否也来自GraphQL查询。

这里的查询

{ 
    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...(任何地方)

回答

0

只好把$或里面的地方添加一个美元符号。

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']], 
     }); 
    } 
    } 
} 
0

你总是可以传递一个可选的where对象过滤查询。见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, 
      }, 
      }] 
     }); 
    } 
    } 
} 
相关问题