
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
type-graphql-filter
Advanced tools
Filter decorator for type-graphql.
It will allow you to generate standardized filterable queries simply by decorating the fields that should be filterable with @Filter
, and decorating the relevant query with an extra @Arg
decorator.
The input types for your schema will be generated automatically based on the name of your type-graphql ObjectType
.
It is useful if you have several type of filterable entities in your schema and you want to provide a unified interface to filter them without having to maintain and duplicate the logic and the InputTypes manually.
Just add the @Filter
decorator to the fields that should be filterable.
import { Field, ObjectType, GraphQLISODateTime, Int } from "type-graphql";
import { Filter } from "type-graphql-filter";
@ObjectType("Example")
export class ExampleModel {
@Field()
@Filter(["eq", "ne", "like", "likeAny", "in"])
myField: string;
@Field(type => Int)
@Filter(["lt", "gt", "eq", "ne", "in"], type => Int)
numberField: number;
@Field(type => GraphQLISODateTime)
@Filter(["eq", "ne", "gt", "lt"], type => GraphQLISODateTime)
dateField: Date;
}
Then add the filter as an argument to your resolver.
import { Resolver, FieldResolver, Root, Arg } from "type-graphql";
import { generateFilterType } from "type-graphql-filter";
import { ExampleModel } from "./models";
@Resolver(of => Parent)
export class ParentExamplesResolver {
@FieldResolver(type => ExampleModel[])
examples(
@Root() parent: Parent,
// add the filter here as parameter
@Arg("filter", generateFilterType(ExampleModel))
filter: any
) {
return new ExampleLoader().load({
filter // handle the filter with your business logic
});
}
}
This will automatically generate the InputType
:
type ExampleFilter {
...
}
The goal of this module is to generate the filter types for your graphql schema, however it is not aware of your business logic, so what you do with the filter you received from the query is up to you.
For a simplified example, you could consider writing such a function, passing it the filter object:
const generateCondition = (filter) => {
for (const [filterKey, value] of Object.entries(filters)) {
const [field, operator] = filterKey.split("_");
// from that point on, if the filter in your query was { firstname_like: "john" } you have:
// - the name of the field as 'field' variable, i.e. "firstname"
// - the operator to filter this field as 'operator' variable, i.e. "like"
// - the value of the filter as 'value' variable, i.e. "john"
}
}
FAQs
Filter decorator for type-graphql
The npm package type-graphql-filter receives a total of 135 weekly downloads. As such, type-graphql-filter popularity was classified as not popular.
We found that type-graphql-filter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.