Graphql 字段上的未知参数 [英] Graphql Unknown argument on field

查看:28
本文介绍了Graphql 字段上的未知参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to GraphQL. Whenever I try to send args on (posts) child node like the query below, I get an error msg "Unknown argument id on field posts of type user". I want to bring a particular post only, not all of them.

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

这是我的 Schema.js 文件..

Here is my Schema.js file ..

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;

推荐答案

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

looks like you are missing args on users, hence, it should look like this:

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


    }
  }
});

这篇关于Graphql 字段上的未知参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆