2016-09-25 31 views
4

我是新的GraphQL,當我嘗試發送像下面的查詢這樣的子節點上的參數時,我得到了一個錯誤消息「用戶類型字段帖子上的未知參數ID」。我想帶一些特定的帖子而不是全部。Graphql關於字段的未知參數

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

這裏是我的Schema.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 Schama = new graphql.GraphQLSchema({ 
    query : query, 
    mutation : Mutation 
}) 

module.exports = Schama; 
+0

什麼是您的架構看起來像?很可能你沒有在模式中正確聲明id參數。 – helfer

+0

我添加了我的schema.js文件,請檢查一次@helfer –

回答

5

看起來你是在用戶缺少ARGS,因此,它應該是這樣的:

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:{ 
     args: { 
     id:{ 
      type : graphql.GraphQLInt, 
     }, 
     type: new graphql.GraphQLList(posts), 
     resolve(user, args){ 
      // Code here to use args.id 
      return user.getPosts(); 
     } 
     } 


    } 
    } 
}); 
+0

謝謝@HagaiCo –