Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphql Unknown argument on field

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
              }
            }
          }
        }

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;
like image 444
Achyut Kr Deka Avatar asked Sep 25 '16 19:09

Achyut Kr Deka


3 Answers

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


    }
  }
});
like image 110
HagaiCo Avatar answered Oct 31 '22 00:10

HagaiCo


You this error if the argument does not exist in the field.

For example, in this case, invalidArg does not exist in the list. The only possible arguments are last, after, first, before, orderBy and ownedByViewer. If you try to pass anything else, you will get an error.

enter image description here

like image 23
live-love Avatar answered Oct 31 '22 00:10

live-love


Try replacing posts (plural) with post (singular).

post(id:2) {
          title
          tags {
            name
          }
        }
like image 1
ggootz Avatar answered Oct 31 '22 00:10

ggootz