[英]Graphql Unknown argument on field
I am new to GraphQL.我是 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".
每当我尝试像下面的查询那样在(帖子)子节点上发送 args 时,我都会收到错误消息“用户类型的字段帖子上的未知参数 ID”。 I want to bring a particular post only, not all of them.
我只想带一个特定的帖子,而不是全部。
{ people(id:[1,2]) {
id
username
posts(id:2) {
title
tags {
name
}
}
}
}
Here is my Schema.js file ..这是我的 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;
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();
}
}
}
}
});
You this error if the argument does not exist in the field.如果该字段中不存在该参数,则会出现此错误。
For example, in this case, invalidArg does not exist in the list.例如,在这种情况下,列表中不存在 invalidArg。 The only possible arguments are last, after, first, before, orderBy and ownedByViewer.
唯一可能的参数是 last、after、first、before、orderBy 和 ownedByViewer。 If you try to pass anything else, you will get an error.
如果您尝试传递其他任何内容,则会出现错误。
Try replacing posts
(plural) with post
(singular).尝试用
post
(单数)替换posts
(复数)。
post(id:2) {
title
tags {
name
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.