EntityAssigner not using proper EntityManager
See original GitHub issueI am using ApolloServer
and passing a forked em
into the ctx: Context
. I am watching sql queries with debug mode on and notice that for a very simple case when fetching relationships there are fewer queries than expected, meaning that somehow my forked em
is being polluted with global context from previous requests.
I am still familiarizing myself with the code, but I believe the error might stem from EntityAssigner L15
static assign<T extends AnyEntity<T>>(entity: T, data: EntityData<T>, onlyProperties: AssignOptions | boolean = false): T {
const em = wrap(entity).__em;
...
which is called by EntityManager L222
// add to IM immediately - needed for self-references that can be part of `data` (and do not trigger cascade merge)
this.getUnitOfWork().merge(entity, [entity]);
EntityAssigner.assign(entity, data as EntityData<T>, true);
this.getUnitOfWork().merge(entity); // add to IM again so we have correct payload saved to change set computation
EntityAssigner.assign
is accessing a different instance of em
that has more objects defined in its unitOfWork.identityMap
than the forked instance of em
that we had been using in EntityManager.merge
previously in the call stack.
Here is the relevant portion of my index.ts
just to show you that I am following all of your suggestions and still having this issue:
app.use((req, res, next) => {
RequestContext.create(orm.em, next);
});
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [UserResolver]
}),
context: ({ req, res }) => {
const em = orm.em.fork()
em.clear()
// confirming that each request starts with a clean map
console.log(em.getUnitOfWork().getIdentityMap())
return {
req,
res,
em,
}
}
});
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (6 by maintainers)
Ok so the problem is that the
em.merge()
is actually not correcting the internal__em
value as it should be (not for the user entity because it was not looked up by primary key).Will try to publish new version soon.
Thanks for the repro, that will help a lot! Will try to look into this soon.