【问题标题】:Graphql Unknown argument on fieldGraphql 字段上的未知参数
【发布时间】:2017-02-03 02:31:14
【问题描述】:

我是 GraphQL 的新手。每当我尝试像下面的查询一样在(帖子)子节点上发送参数时,我都会收到错误消息“用户类型的字段帖子上的未知参数 id”。我只想带一个特定的帖子,而不是全部。

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

【问题讨论】:

  • 您的架构是什么样的?很可能您没有在架构中正确声明 id 参数。
  • 我已经添加了我的 schema.js 文件,请检查一次@helfer

标签: javascript graphql


【解决方案1】:

看起来您缺少用户的参数,因此,它应该如下所示:

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();
        }
      }


    }
  }
});

【讨论】:

  • 由于您的提示,我不再遇到嵌套类型上的红色漩涡问题,谢谢!但是,在我的示例中,我必须将 args-tag 放在我的 INPUT 定义上,而不是 OUTPUT 定义上,就像您在上面给出的示例一样...也许 Achyat 不需要同时使用两者...?此外,您在类型之前缺少一个大括号,因为我认为 args 不应包含类型和解析器......我只是想为那些可能处于类似情况的人指出这一点......
【解决方案2】:

如果参数在字段中不存在,则会出现此错误。

例如,在这种情况下,invalidArg 不存在于列表中。唯一可能的参数是 last、after、first、before、orderBy 和ownedByViewer。如果你尝试传递其他任何东西,你会得到一个错误。

【讨论】:

    【解决方案3】:

    尝试将posts(复数)替换为post(单数)。

    post(id:2) {
              title
              tags {
                name
              }
            }
    

    【讨论】:

      猜你喜欢
      • 2017-09-06
      • 2020-05-18
      • 2020-02-18
      • 2019-03-28
      • 2016-03-13
      • 2020-11-27
      • 2020-11-28
      • 2022-08-05
      • 2020-11-01
      相关资源
      最近更新 更多