【问题标题】:GraphQL, Mysql equivalent OR operationGraphQL、Mysql等价OR运算
【发布时间】:2017-02-03 01:31:54
【问题描述】:

我刚刚学习了 GraphQL,我想找到用户 id=2 OR 用户 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;

【问题讨论】:

  • 您想要实现一个可以向users() 提供ids 数组以获取的情况?
  • 是的,我想获取一个 id 数组

标签: javascript graphql


【解决方案1】:

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

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

如果有帮助请告诉我:)

【讨论】:

  • 谢谢@Kesem David .. 我还有另一个问题,请检查我编辑的问题一次
  • 请立即查看
  • 我很乐意为您提供帮助,让我们保留 stackoverflow 道德规范,删除此编辑并打开一个新问题,我们将继续讨论 :)
  • 你好@Kesem 我打开了一个新问题stackoverflow.com/questions/39691111/…,请检查一次,谢谢..
猜你喜欢
  • 2020-02-17
  • 2016-07-29
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 2015-07-08
相关资源
最近更新 更多