2016-09-25 33 views
0

我剛剛瞭解到GraphQL,我想查找用戶id = 2 user id = 3現在我將如何製作GraphQL查詢,我正在使用以下查詢獲取整個集合GraphQL,Mysql相當於或操作

{ 
     users() { 
     id 
     username 
     posts { 
      title 
      tags { 
      name 
      } 
     } 
     } 
    } 

第2期 -

{ 
      people(id:[1,2,3]) { 
      id 
      username 
      posts(id:2) { 
       title 
       tags { 
       name 
       } 
      } 
      } 
     } 

如果我添加上,然後提交帖子的ARG我得到一個錯誤消息「的類型用戶的外地職位未知參數ID」這是我的架構js文件

var graphql = require('graphql'); 
var Db = require('./db'); 



var users = new graphql.GraphQLObjectType({ 
    name : 'user', 
    description : 'this is user info', 
    fields : function(){ 
    return { 
     id :{ 
     type : graphql.GraphQLInt, 
     resolve(user){ 
      return user.id; 
     } 
     }, 
     username :{ 
     type : graphql.GraphQLString, 
     resolve(user){ 
      return user.username; 
     } 
     }, 

     posts:{ 
     id:{ 
      type : graphql.GraphQLString, 
      resolve(post){ 
      return post.id; 
      } 
     }, 
     type: new graphql.GraphQLList(posts), 
     resolve(user){ 
      return user.getPosts(); 
     } 
     } 


    } 
    } 
}); 



var posts = new graphql.GraphQLObjectType({ 
    name : 'Posts', 
    description : 'this is post info', 
    fields : function(){ 
    return { 
     id :{ 
     type : graphql.GraphQLInt, 
     resolve(post){ 
      return post.id; 
     } 
     }, 
     title :{ 
     type : graphql.GraphQLString, 
     resolve(post){ 
      return post.title; 
     } 
     }, 
     content:{ 
     type : graphql.GraphQLString, 
     resolve(post){ 
      return post.content; 
     } 
     }, 
     person :{ 
     type: users, 
     resolve(post){ 
      return post.getUser(); 
     } 
     }, 

     tags :{ 
     type: new graphql.GraphQLList(tags), 
     resolve(post){ 
      return post.getTags(); 
     } 
     } 
    } 
    } 
}); 

var tags = new graphql.GraphQLObjectType({ 
    name : 'Tags', 
    description : 'this is Tags info', 
    fields : function(){ 
    return { 
     id :{ 
     type : graphql.GraphQLInt, 
     resolve(tag){ 
      return tag.id; 
     } 
     }, 
     name:{ 
     type : graphql.GraphQLString, 
     resolve(tag){ 
      return tag.name; 
     } 
     }, 
     posts :{ 
     type: new graphql.GraphQLList(posts), 
     resolve(tag){ 
      return tag.getPosts(); 
     } 
     } 
    } 
    } 
}); 

var query = new graphql.GraphQLObjectType({ 
    name : 'query', 
    description : 'Root query', 
    fields : function(){ 
    return { 
    people :{ 
     type : new graphql.GraphQLList(users), 
     args :{ 
      id:{type: new graphql.GraphQLList(graphql.GraphQLInt)}, 
      username:{ 
      type: graphql.GraphQLString 
      } 
     }, 
     resolve(root,args){ 
      return Db.models.user.findAll({where:args}); 
     } 
     }, 

     posts:{ 
     type : new graphql.GraphQLList(posts), 
     args :{ 
      id:{ 
      type: graphql.GraphQLInt 
      }, 
      title:{ 
      type: graphql.GraphQLString 
      }, 
     }, 
     resolve(root,args){ 
      return Db.models.post.findAll({where:args}); 
     } 
     }, 

     tags :{ 
     type : new graphql.GraphQLList(tags), 
     args :{ 
      id:{ 
      type: graphql.GraphQLInt 
      }, 
      name:{ 
      type: graphql.GraphQLString 
      }, 
     }, 
     resolve(root,args){ 
      return Db.models.tag.findAll({where:args}); 
     } 
     } 

    } 
    } 

}); 

var Mutation = new graphql.GraphQLObjectType({ 
    name : "mutation", 
    description : 'function for mutaion', 
    fields : function(){ 
    return { 
     addPerson : { 
     type : users, 
     args :{ 
      username : { 
      type : new graphql.GraphQLNonNull(graphql.GraphQLString) 
      }, 
      email :{ 
      type : new graphql.GraphQLNonNull(graphql.GraphQLString) 
      } 
     }, 
     resolve(_, args){ 
      return Db.models.user.create({ 
      username : args.username, 
      email : args.email 
      }); 
     } 
     } 
    } 
    } 
}) 

var Schama = new graphql.GraphQLSchema({ 
    query : query, 
    mutation : Mutation 
}) 

module.exports = Schama; 
+1

你想實現,你可以提供給用戶'的情況下,() '一個'id's數組來獲取? –

+0

是的,我想獲取一個ID數組 –

回答

1

爲了使用id秒的陣列來從您的架構多數據,你應該定義您的模式給予users的ARGS如下:

fields:() => ({ 
     users: { 
      type: new GraphQLList(USER_GRAPHQL_OBJECT_TYPE), 
      args: { 
       id: {type: new GraphQLList(GraphQLInt)} 
      }, 
      resolve: (root, args) => { 
       // fetch users 
      } 
     } 
    }) 

注意new GraphQLList包裝GraphQLInt類型的id。

然後,查詢您的架構可以當:

{ 
    users(id: [2, 3]) { 
    id 
    username 
    posts { 
     title 
     tags { 
     name 
     } 
    } 
    } 
} 

請讓我知道,如果它是有幫助:)

+0

謝謝@Kesem大衛..我有另一個問題,請檢查我編輯的問題一次 –

+0

@AchyutKrDeka我似乎無法找到編輯 –

+0

請現在檢查 –