slaypni / type-graphql-dataloader

TypeGraphQL + DataLoader + TypeORM made easy
https://www.npmjs.com/package/type-graphql-dataloader
MIT License
151 stars 17 forks source link

Allow specifying multiple connections or a ConnectionManager in ApolloServerLoaderPlugin #11

Open mscofield0 opened 3 years ago

slaypni commented 3 years ago

What kind of use cases do you assume?

rb123454321 commented 3 years ago

I have a sqlite in-memory database and a PostgreSQL persistent database at the same time.

malisetti commented 3 years ago

Can you try this PR and see if it works for you.

https://github.com/slaypni/type-graphql-dataloader/pull/14

DillonSadofsky commented 2 years ago

I think maybe my use case is different than the OP, but I think passing context into typeormGetConnection in ImplicitLoaderImpl would allow people like myself who want all queries for a given request to go over the same connection to be possible.

I do some initialization of connections from the pool by setting mysql connection variables, so a connection has been pulled from the pool and stored on the context. I can make all other load operations use this context connection, but since the data-loaders use the top level pool, they will 'randomly' grab another connection, which can cause issues with mysql connection TIME_ZONE, etc. I think this would be as simple as changing line 37 of ImplicitLoaderImpl.js from:

            const relation = tgdContext
                .typeormGetConnection()
                .getMetadata(target.constructor)
                .findRelationWithPropertyPath(propertyKey.toString());

to:

            const relation = tgdContext
                .typeormGetConnection(context)
                .getMetadata(target.constructor)
                .findRelationWithPropertyPath(propertyKey.toString());

This would give a program access to any context-specific information/connections/etc when handling the loading operations for a given request.