I want to create a recursive input for one of my graphql queries and add filters to have the possibility to create groups of filters.
A basic filter must have an id, a value and an operator.
A group of filters must have a condition (and/or) and a filters array which is either a basic filter or another group filters.
This is what I’ve done so far.
import { ArgsType, createUnionType, Field, InputType } from '@nestjs/graphql';
@InputType()
class MyFilterInput {
@Field()
id: string;
@Field(() => String, { nullable: true })
value: string | null;
@Field(() => String)
operator: string;
}
@InputType()
class MyFilterGroupInput {
@Field(() => String)
condition: string;
@Field(() => [MyFilterGroupOrFilterInput])
filters: Array<typeof MyFilterGroupOrFilterInput>;
}
const MyFilterGroupOrFilterInput = createUnionType({
name: 'MyFilterGroupOrFilter',
types: () => [MyFilterInput, MyFilterGroupInput] as const,
resolveType(value) {
if (value.id) {
return MyFilterInput;
}
if (value.condition) {
return MyFilterGroupInput;
}
return null;
},
});
@ArgsType()
export class MyPaginationInput {
@Field(() => [MyFilterGroupInput], { nullable: true })
readonly filters?: MyFilterGroupInput[];
@Field({ nullable: true })
readonly search?: string;
}
When starting the server I have this error:
Error: Cannot determine a GraphQL input type null for the "filters". Make sure your class is decorated with an appropriate decorator.
I have a feeling it is because the union type references the class which also references the union, becoming a cyclic dependency.
How can I solve this?
Leonidas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.