Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create filters for nested classes. #18

Open
doandreas opened this issue Oct 29, 2021 · 1 comment
Open

Create filters for nested classes. #18

doandreas opened this issue Oct 29, 2021 · 1 comment

Comments

@doandreas
Copy link

Thank you for open sourcing a library like this. 👍

I guess from the implementation its not supported to create filters for nested classes right ? I saw a related PR but just to be sure.

Example :

class Person {

name : string ,

address : Address

}

class Address {

postcode:sting,

address:string

}

in order to create a filter like address_postcode_like = "1234"

@pgrund
Copy link
Contributor

pgrund commented Nov 17, 2021

Hi,
I assume your classes are type classes (@ObjectType) ...
I actually do something quite similar (but way more complex) in one of my projects ... so let me share my approach

typescript example:

@ObjectType
export class Person {
  @Field(type => String)
  @Filter(["eq","like"], type => String)
  name: string;
  address_ref: string
}

@ObjectType 
export class Address {
 @Field(type => String)
 @Filter(["eq","like"], type => String)
 postcode:string;

 @Field(type => String)
 address:string;
}

@Resolver(Person)
export class PersonResolver { 
  @FieldResolver(
  address(@Root() person: Person, @Ctx() context): Address {
    return person.address_ref && context.adresses.getByRef(person.address_ref);
  }
}

@InputType()
export class FilterOnPerson extends createFilterType(Person) {resolver?: PersonResolver;
   constructor(propMap: object = {}) {
        super();
        this.resolver = new PersoResolver();
        Object.getOwnPropertyNames(propMap).forEach(name => {
            this[name] = propMap[name];
        })    
    }

   @Field( type => FilterOnAddress )
    address: FilterOnAddress
} 

That allows me within my filter function to delegate filtering to a FilterOnAddress, doing something like

...
switch(field){
    case "address":
        let aFilter = <AddressFilterType>value;
        predicate = ((person: Person, _1, _2) => {
            let address = this.resolver.maintainer(person, context);
            return address && aFilter.filter(context, address);
        })
....

not runnable and a lot of code left out, but I hope it shows the idea ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants