Type-graphql, Mikororm Query returns Ref rather than User

Solution for Type-graphql, Mikororm Query returns Ref rather than User
is Given Below:

I am working with a project backend of creating a clone version of Reddit using Mikro-ORM, Type-graphql, NodeJs, Express, Redis, and PostgreSQL

This is my Initial Project Setup with Mikororm, connect it to Postgres, and Initiate Redis for caching

const orm = await MikroORM.init(microConfig);

  // run the Migration before do anything else
  await orm.getMigrator().up();

  // create an express server app
  const app = express();

  // set up redis running
  const RedisStore = connectRedis(session);
  const redis = new Redis();
  app.use(
    cors({
      origin: "http://localhost:3000",
      credentials: true,
    })
  );
  app.use(
    session({
      name: COOKIE_Name,
      store: new RedisStore({
        client: redis,
        disableTTL: true,
      }),
      cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 365 * 3, // 3 years cookies
        httpOnly: true,
        secure: __prod__, // cookie only works in https
        sameSite: "lax", //csrf: google this for more information
      },
      saveUninitialized: false,
      secret: "nkadniandiuanidnaidhaiddwq",
      resave: false,
    })
  );

  app.get("/", (_, res) => {
    res.send("hello");
  });
  app.listen(4000, () => {
    console.log("server started on localhost:4000");
  });

  // Apollo server
  const apolloServer = new ApolloServer({
    schema: await buildSchema({
      resolvers: [HelloResolver, PostResolver, UserResolver],
      validate: false,
    }),
    context: ({ req, res }): MyContext => ({ em: orm.em, req, res, redis }),
  });

  apolloServer.applyMiddleware({
    app,
    cors: false,
  });
};

My Entity set up for Post.js

@ObjectType()
@Entity()
export class Post {
  @Field()
  @PrimaryKey()
  _id!: number;

  @Field()
  @Property()
  creatorId!: number;

  @Field()
  @Property({ type: "date" })
  createdAt: Date = new Date();

  @Field()
  @Property({ type: "date", onUpdate: () => new Date() })
  updatedAt: Date = new Date();

  @Field()
  @Property({ type: "text" })
  title!: string;

  @Field()
  @Property({ type: "text" })
  text!: string;

  @Field()
  @Property({ type: "number" })
  points!: number;

  @Field()
  @ManyToOne(() => User)
  creator: User;

  constructor(creator: User) {
    this.creator = creator;
  }

  @OneToMany(() => Upvote, (upvote) => upvote.post)
  upvote = new Collection<Upvote>(this);
}

And User.js

@ObjectType()
@Entity()
export class User {
  @Field()
  @PrimaryKey()
  @Property()
  _id!: number;

  @Field()
  @Property({ type: "date" })
  createdAt: Date = new Date();

  @Field()
  @Property({ type: "date", onUpdate: () => new Date() })
  updatedAt: Date = new Date();

  @Field()
  @Property({ type: "text", unique: true })
  username!: string;

  @Field()
  @Property({ type: "text", unique: true })
  email!: string;

  @Property({ type: "text" })
  password!: string;

  @OneToMany(() => Post, (post) => post.creator)
  posts = new Collection<Post>(this);

  @OneToMany(() => Upvote, (upvote) => upvote.user)
  upvote = new Collection<Upvote>(this);
}

This is my resolver for querying all the Posts, each post will have a creator (which is User) and one user can have many posts created:

@Query(() => PaginatedPosts)
  async posts(
    @Arg("limit", () => Int) limit: number,
    @Arg("cursor", () => String, { nullable: true }) cursor: string,
    @Ctx() { em }: MyContext
  ) {
    const realLimit = Math.min(50, limit);
    const realLimitPlusOne = realLimit + 1;

    const withCursor = await em
      .getRepository(Post)
      .find(
        { $and: [{ "createdAt <=": cursor }] },
        { limit: realLimitPlusOne, orderBy: { createdAt: "DESC" } }
      );
    const withoutCursor = await em
      .getRepository(Post)
      .find({}, { limit: realLimitPlusOne, orderBy: { createdAt: "DESC" } });

    // console.log("Post with Cursor:", withCursor);

    console.log("Post without Cursor", withoutCursor);

    if (cursor) {
      return {
        posts: withCursor.slice(0, realLimit),
        hasMore: withCursor.length === realLimitPlusOne,
      };
    } else {
      return {
        posts: withoutCursor.slice(0, realLimit),
        hasMore: withoutCursor.length === realLimitPlusOne,
      };
    }
  }

Here’s when the problem occurs, sometimes the Data return to me is just the Ref rather than the User object I wanted

Post {
    _id: 214,
    creatorId: 18,
    createdAt: 2021-07-31T09:48:50.000Z,
    updatedAt: 2021-07-31T09:48:50.000Z,
    title: 'Yay It works ',
    text: ':>>>',
    points: 3,
    creator: Ref<User> { _id: 18 },
    upvote: Collection { initialized: false, dirty: false }
  }

But sometimes it does actually work, but many time it fails the Query because it only returns the Ref<> instead of the whole user object

Post {
    _id: 214,
    creatorId: 18,
    createdAt: 2021-07-31T09:48:50.000Z,
    updatedAt: 2021-07-31T09:48:50.000Z,
    title: 'Yay It works ',
    text: ':>>>',
    points: 3,
    creator: User {
      _id: 18,
      createdAt: 2021-07-19T15:30:31.000Z,
      updatedAt: 2021-07-21T06:55:03.000Z,
      username: 'minhquan0902',
      email: '[email protected]',
      password: '$argon2i$v=19$m=4096,t=3,p=1$powvhc+mI4O/jmhXz807lg$oZtyE2HRW6Ei6JqPGcEDBdmEnv3D81C9lMNd5kmEKPA',
      posts: [Collection],
      upvote: [Collection]
    },

Looks like you are not handling request context anywhere, and therefore reusing the same EM for all requests. Each request needs to have its own EM fork.

See https://mikro-orm.io/docs/identity-map to understand why.

I don’t have experience with apollo, but I believe it should be enough to change this line:

context: ({ req, res }): MyContext => ({ em: orm.em, req, res, redis }),

to use new fork instead of the global orm.em

context: ({ req, res }): MyContext => ({ em: orm.em.fork(), req, res, redis }),

Here is an example app that does it this way:
https://github.com/driescroons/mikro-orm-graphql-example/blob/master/src/application.ts#L63