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.