Graphql 字段上存在未知参数

问题描述 投票:0回答:3

我是 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;
javascript graphql
3个回答
7
投票

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

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
投票

尝试将

posts
(复数)替换为
post
(单数)。

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

0
投票

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

例如,在本例中,列表中不存在

invalidArg
。唯一可能的参数是
last
after
first
before
orderBy
ownedByViewer
。如果您尝试传递其他任何内容,您将收到错误。

© www.soinside.com 2019 - 2024. All rights reserved.