nestjs-query icon indicating copy to clipboard operation
nestjs-query copied to clipboard

Filtering by relation field not working when custom relation name used

Open vladmelnyk opened this issue 4 years ago • 9 comments

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:

  1. Change FilterableRelation first param 'name' to some custom one
  2. 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 avatar Dec 15 '20 01:12 vladmelnyk

@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

smolinari avatar Dec 15 '20 07:12 smolinari

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

vladmelnyk avatar Dec 15 '20 11:12 vladmelnyk

Hi @vladmelnyk,

Would you mind providing your DTO and entity definitions?

doug-martin avatar Dec 15 '20 16:12 doug-martin

@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\""

vladmelnyk avatar Dec 16 '20 19:12 vladmelnyk

Thank you for the example. Have you tried the relationName option?

doug-martin avatar Dec 16 '20 20:12 doug-martin

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 avatar Dec 19 '20 17:12 vladmelnyk

@vladmelnyk - Shouldn't your relation filter then be training_event?

Scott

smolinari avatar Dec 20 '20 03:12 smolinari

no

vladmelnyk avatar Dec 22 '20 15:12 vladmelnyk

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: image

But filtering by any properties of the relation (abbreviation, language) does yield an error: image

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?

zampage avatar May 27 '21 18:05 zampage