简体   繁体   English

Graphql 字段上的未知参数

[英]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.

相关问题 收到错误:类型为“查询” [GraphQL,Relay,React]的字段“用户”上的未知参数“ id” - Getting error : Unknown argument “id” on field “user” of type “Query” [GraphQL, Relay, React] 为什么graphQl在尝试切片查询时返回“Unknown argument 'first' on field...”错误? - Why graphQl returns “Unknown argument 'first' on field…” error when trying to slice on query? 类型为“ Mutation \\”的字段“ forgotPassword \\”上的“未知参数\\” email \\”。” - “Unknown argument \”email\“ on field \”forgotPassword\“ of type \”Mutation\“.” [GraphQL 错误]:消息:未知片段 - [GraphQL error]: Message: Unknown fragment GraphQL 字段带空格 - GraphQL field with space GraphQL - 全局字段解析器 - GraphQL - Global field resolvers Nestjs graphql 场卫 - Nestjs graphql field guard Apollo GraphQL 变量参数名称 - Apollo GraphQL variable argument name CombinedErrors graphQLErrors Unknown argument limit on field Query.posts - 尝试使用 urql、Strapi、Next JS 将参数传递给查询 - CombinedErrors graphQLErrors Unknown argument limit on field Query.posts - Trying to pass argument to query using urql, Strapi, Next JS GraphQL 查询中的错误显示未知类型“ContentfulSizes” - Error in GraphQL query it says Unknown type "ContentfulSizes"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM