nestjs-query
nestjs-query copied to clipboard
Filtering by relation field not working when custom relation name used
Describe the bug
Following guide
works fine until I rename relation to something custom:
If I call it todoItemRelation
for example - I will get the following error (I specified relationName: 'todoItem' as well):
when trying to filter on todoItem id field
unknown operator \"id\""
Have you read the Contributing Guidelines?
(Write your answer here.)
To Reproduce Steps to reproduce the behavior:
- Change FilterableRelation first param 'name' to some custom one
- Filter on the relation name field
Expected behavior
No errors when running
{ subTasks(filter: { todoItem: { title: { like: "Create%" } } }) { title completed } }
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- Node Verson [v15.2.1]
- Nestjs-query Version [e.g. v0.21.2]
Additional context Add any other context about the problem here.
@vladmelnyk - Can you show your code and how you ran your query? I ask, because you mention filtering on id, yet show a filter for title.
Scott
oh, sorry @smolinari, for that particular example that I submitted - it would be this error:
unknown operator \"title\""
Ultimately, it doesn't really matter on what field I apply filter - it is always show me this error. I can only fix it by keeping the original relation name
Hi @vladmelnyk,
Would you mind providing your DTO and entity definitions?
@doug-martin
dto: `@ObjectType('TrainingBookOrder') @FilterableRelation('training_event', () => TrainingEventDTO, { disableRemove: false, disableUpdate: false, disableRead: false, allowFiltering: true }) export class TrainingBookOrderDTO implements ITrainingBookOrder { @FilterableField(id => ID) id: number;
@Field(() => FormattedTrainingEventDto) training_event: ITrainingEvent;
@FilterableField()
training_event_id: number;
}
entity:
@Entity()
export class TrainingBookOrderEntity implements ITrainingBookOrder {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne(type => TrainingEventEntity, { nullable: true }) @JoinColumn({ name: 'training_event_id' }) training_event: ITrainingEvent;
@Column() training_event_id: number; }`
and the graphql query
query { trainingBookOrders( paging: { first: 50 } filter: { training_event: { id: { eq: 15 } } } ) { edges{ node{ id } } } }
So this one actually works, but if you change reltaion name to some custom one (changing query accordingly) it will fail with
unknown operator \"id\""
Thank you for the example. Have you tried the relationName
option?
I tried this in dto
@FilterableRelation('training_event_custom_name', () => TrainingEventDTO, { disableRemove: false, disableUpdate: false, disableRead: false, allowFiltering: true, relationName: 'training_event' })
query:
query { trainingBookOrders( paging: { first: 50 } filter: { training_event_relation2: { id: { eq: 15 } } } ) { edges{ node{ id training_event_relation2 {id} } } } }
result:
"message": "unknown operator \"id\"",
@vladmelnyk - Shouldn't your relation filter then be training_event
?
Scott
no
Not sure if this is the same issue, but I get the same error when trying to filter fields of a filterable relation. I'm using mongoose schemas, and can provide some code and outputs:
Questionnaire
@ObjectType()
@Schema({ collection: 'questionnaires', timestamps: {} })
export class Questionnaire extends Document {
@Field(() => String)
_id: Types.ObjectId;
@FilterableField(() => String)
@Prop()
language: string;
@FilterableField(() => String)
@Prop()
abbreviation: string;
}
export const QuestionnaireSchema = SchemaFactory.createForClass(
Questionnaire,
).index({ language: 1, abbreviation: 1 }, { unique: true });
QuestionnaireSchema.virtual('questionnaireVersions', {
ref: 'questionnaireversion',
localField: '_id',
foreignField: 'questionnaire',
});
QuestionnaireVersion
@ObjectType()
@FilterableRelation('questionnaire', () => Questionnaire, { nullable: true, disableRemove: true })
@Schema({ collection: 'questionnaire_versions', timestamps: {} })
export class QuestionnaireVersion extends Document {
@Field(() => String)
_id: Types.ObjectId;
@Prop({ type: Types.ObjectId, ref: Questionnaire.name })
questionnaire: Types.ObjectId | Questionnaire;
@FilterableField(() => String)
@Prop()
name: string;
}
export const QuestionnaireVersionSchema = SchemaFactory.createForClass(
QuestionnaireVersion,
);
Filtering by name works fine:
But filtering by any properties of the relation (abbreviation, language) does yield an error:
Generated inputs in the schema.gql
input QuestionnaireVersionFilter {
and: [QuestionnaireVersionFilter!]
or: [QuestionnaireVersionFilter!]
name: StringFieldComparison
questionnaire: QuestionnaireVersionFilterQuestionnaireFilter
}
input QuestionnaireVersionFilterQuestionnaireFilter {
and: [QuestionnaireVersionFilterQuestionnaireFilter!]
or: [QuestionnaireVersionFilterQuestionnaireFilter!]
language: StringFieldComparison
abbreviation: StringFieldComparison
}
Any idea what I might be doing wrong? or is this a bug?