我试图找出编写一个过滤多个参数的解析器的最佳方法 . 我有以下graphql类型

const userQuery = new GraphQLObjectType({
    name: 'Query',
    fields: {
        Users: {
            type: new GraphQLList(User),
            args: {
                userId: { type: GraphQLString }
            },
            resolve: function (_, { UserId}) {
                return new Promise((resolve, reject) => {
                   //Code to query the data store for the user with the given UserId
                })
            }
        }
    }
});

用户类型具有以下字段

  • 姓名

  • UserId

  • 类型

  • 性别

现在,如果我想介绍基于名称过滤用户的能力,那么最好的方法是什么 . 我能想到的唯一方法是修改解析器以包含其他args,然后根据传入的内容将其发送到数据库 . 例如

const userQuery = new GraphQLObjectType({
    name: 'Query',
    fields: {
        Users: {
            type: new GraphQLList(User),
            args: {
                userId: { type: GraphQLString }
            },
            resolve: function (_, { UserId, name}) {
                return new Promise((resolve, reject) => {
                   //Check which argument is passed in and then run the query against the datastore
                })
            }
        }
    }
});

有没有更好的方法来做到这一点?如果我希望用户能够过滤另一个属性,那么它会变得更加复杂,并且解析功能将变得庞大而复杂 .