-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose resolveContainer and createContainer to public? #2
Comments
I guess that would be possible, probably by exposing an Application class with some Methods. I will take a look at this tomorrow! Is your DataLoader a Module/Service? In that case you could simply inject the connection into it. |
I initially implemented it as a service but then dataloaders should be created on a per request basis so I have to instantiate it multiple times. |
Would something like this work for you? // Dummy class for connection infos
class Connection {}
// Your real DataLoader implementation which utilizes a connection
class DataLoader() {
constructor(private final connection: Connection) {}
}
// A wrapper around your DataLoader, which creates a fresh instance on demand
class DataLoaderFactory {
@Inject(Connection)
connection!: Connection;
public create() {
return new DataLoader(this.connection);
}
}
// Dummy consumer
class Consumer {
@Inject(DataLoaderFactory)
dataLoaderFactory!: DataLoaderFactory;
@PostModuleInit()
public postModuleInit() {
let dataLoader = dataLoaderFactory.create();
// Work with fresh instance of DataLoader
}
}
// Inject only Connection and Factory as providers
@Module({
providers: [Connection, DataLoaderFactory],
consumers: [Consumer],
})
class Module {}
new Application(Module).boot(); Please notice, there will come some new features in the next version featuring an application class. Also @OnModuleInit will not exist anymore and gets replaced by Hooks (@PostModuleInit). |
This can work, in my case, I would call |
I think doing it this way is much cleaner. It would be a very "hacky" way implementing any method to resolve a provider outside of the modules. |
I'm currently using this library with apollo graphql, I would like to create a DataLoader on each request. But in order to do that, DataLoader depends on a DBConnection.
It would be nice if we can reuse existing instantiated classes by using
resolveContainer
.The text was updated successfully, but these errors were encountered: