Achyut Kr Deka Achyut Kr Deka - 2 months ago 18
Javascript Question

GraphQL, Mysql equivalent OR operation

I have just learnt GraphQL and I want to find the user id=2 OR user id=3 now how will I make the GraphQL query,I am getting the whole set using the bellow query

{
users() {
id
username
posts {
title
tags {
name
}
}
}
}


2nd issue --

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


if I add the arg on Posts filed then i got an error msg "Unknown argument id on field posts of type user"

This 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 Mutation = new graphql.GraphQLObjectType({
name : "mutation",
description : 'function for mutaion',
fields : function(){
return {
addPerson : {
type : users,
args :{
username : {
type : new graphql.GraphQLNonNull(graphql.GraphQLString)
},
email :{
type : new graphql.GraphQLNonNull(graphql.GraphQLString)
}
},
resolve(_, args){
return Db.models.user.create({
username : args.username,
email : args.email
});
}
}
}
}
})

var Schama = new graphql.GraphQLSchema({
query : query,
mutation : Mutation
})

module.exports = Schama;

Answer

In order to fetch multiple data from your schema using an array of ids you should define the args given to the users in your schema as follows:

fields: () => ({
        users: {
            type: new GraphQLList(USER_GRAPHQL_OBJECT_TYPE),
            args: {
                id: {type: new GraphQLList(GraphQLInt)}
            },
            resolve: (root, args) => {
                // fetch users
            }
        }
    })

notice the new GraphQLList wrapping the GraphQLInt type of the id.

Then, when querying your schema you can :

{
  users(id: [2, 3]) {
    id
    username
    posts {
      title
      tags {
        name
      }
    }
  }
}

Please let me know if it was helpful :)