2016-09-25 78 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

请现在检查 –