typescriptgraphqltypeormtypegraphqlentityresolver

TypeORM 0.3.12: FieldResolver() not returning expected results with where statement


I recently upgraded TypeORM from version 0.2.37 to 0.3.12. However, after the upgrade, the where statement is not returning any answers' entity even though there are records in the database that should match the criteria (question is not finding any answer). Reverting to version 0.2.37 resolves the issue.

What could be causing this behavior in TypeORM version 0.3.12? And how can I resolve this issue while still using the latest version of TypeORM?

Here is the code:

📁resolvers/📁types/📄question.ts

import {
  Ctx,
  FieldResolver,
  Resolver,
  ResolverInterface,
  Root,
} from "type-graphql";
import { Context } from "./../../index"
import { Answer } from "../../entities/answer";
import { Question } from "../../entities/question";
import { Posted_Answer } from "../../entities/posted_answer";

@Resolver((of) => Question)
export class Question_Resolver implements ResolverInterface<Question> {
  @FieldResolver()
  async answers(@Root() root: Question, @Ctx() context: Context) {
    const answers = await context.connection.manager.find(Answer, {
      // If I comment the where statement, question can resolve the answers using TypeORM 0.3.12, however, I need to filter them with the where statement.
      where: { question: root },
    });
    return answers;
  }
}

Solution

  • I still don't know why it doesn't work, however, using a different approach make it works again:

      @FieldResolver()
      async answers(@Root() root: Question, @Ctx() context: Context) {
        // const answers = await context.connection.manager.find(Answer, {
        //   where: { question: root },
        // });
    
        const query = createQueryBuilder(Answer, "answer")
          .leftJoinAndSelect("answer.question", "question")
          .where("question.uuid = :questionId", { questionId: root.uuid });
    
        const answers = await query.getMany();
    
        return answers;
      }
    

    |------ UPDATE ------|

    Using only a property to match instead of the whole object make it works!!!

    const answers = await context.connection.manager.find(Answer, {
      where: { question: { uuid: root.uuid } },
    });