Skip to content
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

Open
captainjapeng opened this issue Feb 5, 2023 · 5 comments
Open

Expose resolveContainer and createContainer to public? #2

captainjapeng opened this issue Feb 5, 2023 · 5 comments

Comments

@captainjapeng
Copy link

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.

@DreamTexX
Copy link
Owner

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.

@captainjapeng
Copy link
Author

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.

@DreamTexX
Copy link
Owner

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).

@captainjapeng
Copy link
Author

This can work, in my case, I would call dataLoaderFactory.create() inside the function that handles graphql queries. We could also improve it as well if we'll pass a Context variable to create(ctx) so we can cache the dataloader on a per request/context 😄

@DreamTexX
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants