繁体   English   中英

GraphQL,相当于MySQL的OR操作

[英]GraphQL, Mysql equivalent OR operation

我刚刚学习了GraphQL,现在我想找到用户ID = 2 用户ID = 3,我将如何进行GraphQL查询,使用波纹管查询获取整个集合

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

第二期-

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

如果我在提交的帖子上添加arg,那么我会收到一条错误消息:“类型为user的字段帖子上的未知参数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;

为了使用id数组从架构中获取多个数据,您应该定义在架构中为users提供的arg,如下所示:

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

请让我知道是否有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM