0
votes

Requirement:

I need recommendation in writing resolver function for handling graphql filters. The filter supports eq, ne, like, contains and not like operators.

Schema:

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:

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

Resolvers:

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%'
        //        }
        //    }
        });  
}

Technologies Used:

  • Apollo Server
  • express
  • Sequelize
  • PostgreSQL

How to replace the operators in filter with [Sequelize.Op.operator]? I tried using operatorsAliases in config.json but it didn't have any effect. Your help is much appreciated.

2

2 Answers

2
votes

You can just use bracket notation to access the correct property on Op. Something like this:

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

Bracket notation works even when it's "nested" like this.

1
votes

I solved this problem with OperatorAliases by passing it in Sequelize constructor.

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

Use this Aliases in sequelize while connecting..

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);

And it should work directly in your resolver without any extra effort.

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