如何在 apollo graphql 服务器中编写 resolve graphql 过滤器

How to write resolve graphql filters in apollo graphql server

要求:

我需要有关编写处理 graphql 过滤器的解析器函数的建议。过滤器支持 eq、ne、like、contains 和 not like 运算符。

架构:

import { gql } from 'apollo-server-express';

export default gql`
   extend type Query {
       authGroups(filter: AuthGroupFilterInput): AuthGroupConnection!
    }

    type AuthGroup implements Node {
       id: ID!
       name: String
    }

    input AuthGroupFilterInput {
       name: StringFilterInput
    }

    type AuthGroupConnection {
       edges: [AuthGroup!]!
       pageInfo: PageInfo
    }

    input StringFilterInput {
      lt: String,
      gt: String,
      eq: String,
      ne: String,
      contains: String,
      like: String,
      notLike: String,
    }  

`;

GraphQL 查询:

 Query {
    authGroups(filter: {  
          name: {
             like: '%AD'
          }
       }
      )
    {
       edges {
         id
         name
       }
    }
 }

解析器:

authGroups: async (parent, args, { models }) => {
        const filter = args.filter;
        const filters = filter 
                        ? {
                            where: filter,
                        } 
                        : {};

        const authGroups = await models.authGroup.findAll({
           ...filters
        //    where : {
        //        name: {
        //            [Sequelize.Op.like] : 'AD%'
        //        }
        //    }
        });  
}

使用的技术:

如何用[Sequelize.Op.operator]替换过滤器中的运算符?我尝试在 config.json 中使用 operatorsAliases 但它没有任何效果。非常感谢您的帮助。

您只需使用括号表示法即可在 Op 上访问正确的 属性。像这样:

function transformFilter (filter) {
  for (const fieldName in filter) {
    const condition = filter[fieldName]
    filter[fieldName] = {}
    for (const operatorName in condition) {
      filter[fieldName][Op[operatorName]] = condition[operatorName]
    }
  }
}

即使像这样 "nested" 括号符号也有效。

我通过在 Sequelize 构造函数中传递 OperatorAliases 解决了这个问题。

import Sequelize from 'sequelize';
const operatorsAliases = {
    eq: Op.eq,
    ne: Op.ne,
    gte: Op.gte,
    gt: Op.gt,
    lte: Op.lte,
    lt: Op.lt,
    ......
}

连接时在 sequelize 中使用此别名..

const dbService = {
    connect: (host, database) => {
        const sequelize = new Sequelize(
            database,
            user,
            pwd, {
                host: host,
                dialect: 'yourDialect',
                dialectOptions: {
                    domain: domain,
                    encrypt: true
                },
                operatorsAliases: operatorsAliases,
                pool: {
                    max: 100,
                    min: 10,
                    idle: 200
                }
            }
        );

        sequelize
            .authenticate()
            .then(() => {
                console.log(database, 'Connection has been established successfully.');
            })
            .catch((err) => {
                console.error(err, `Unable to connect to the database.`);
            });

        return sequelize;
    }
};
const yourDBRef = dbService.connect(hostName, dbName);

它应该可以直接在您的解析器中运行,无需任何额外的努力。

  const authGroups = await models.authGroup.findAll({
        where :filters
    });