首页 文章

GraphQL,Mysql等效OR操作

提问于
浏览
0

我刚刚学习了GraphQL,我想找到用户id = 2 OR user id = 3现在我将如何进行GraphQL查询,我使用下面的查询来获取整个集合

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

第二期 -

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

如果我在帖子上添加arg,那么我收到一个错误消息“在用户类型的字段帖子上有未知的参数id”

这是我的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 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;

1 回答

  • 1

    为了使用 id 数组从模式中获取多个数据,您应该在模式中定义给予 users 的args,如下所示:

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

    注意 new GraphQLList 包装了id的 GraphQLInt 类型 .

    然后,在查询架构时,您可以:

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

    如果有帮助请告诉我:)

相关问题