2016-07-31 76 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, 
      }, 
      }] 
     }); 
    } 
    } 
} 
相關問題