-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Support multi option for providers #770
Comments
One use case I have is being able to set my typeDefs through a common injection token. Then during composition of the graphql layer I can compose all the typeDefs in use. Currently, these need to be manually imported. This would aid in modularizing a graphql schema |
Could this support priorities, for ordering imports? It's not in Angular, but similar feature can be found in Symfony DI container.
So now, Symfony documentation: |
Please include this! It looks like a lot of the work has already be done? @BrunnerLivio |
Just a suggestion: it would be nice to be able to pass an abstract class as a token. It'd be one less language element to deal with when using an abstract class as a mere interface. For example: abstract class Flavour {
// The abstract interface here
}
class VanillaFlavour implements Flavour { /* ... */ }
class StrawberryFlavour implements Flavour { /* ... */ }
// ...
@Module({
providers: [
{
provide: Flavour,
useClass: VanillaFlavour,
multi: true,
},
{
provide: Flavour,
useClass: StrawberryFlavour,
multi: true,
}],
})
export class FlavourModule {} Compare with: const FLAVOUR = Symbol('Flavour');
interface Flavour {
// The abstract interface here
}
class VanillaFlavour implements Flavour { /* ... */ }
class StrawberryFlavour implements Flavour { /* ... */ }
// ...
@Module({
providers: [
{
provide: FLAVOUR,
useClass: VanillaFlavour,
multi: true,
},
{
provide: FLAVOUR,
useClass: StrawberryFlavour,
multi: true,
}],
})
export class FlavourModule {} |
It looked like this was changed from todo to core-team in February. Is this being done? I've run into several use cases since I first found this around the beginning of the year where I could have used multi providers. Its a pretty useful and important feature...and would really love to see it in NestJS. |
@jrista this is not a top priority as you can already define providers composed of other providers explicitly (using factory providers). We have a PR in place for this feature as well, there are just several minor things to get done & make sure they work. Let me lock this issue for the time being. |
Just to let everyone know - this won't be implemented as Nest already lets you register composed providers explicitly (non-implicit syntax won't be added). For example: {
provide: 'MY_COMPOSED_PROVIDER',
useFactory: (a, b, c) => [a, b, c],
inject: [A, B, C] // where A, B, C are providers that implements a common interface
} For more information on why the implicit syntax wouldn't be easy to implement & maintain (compared to frameworks like Angular), check out this convo #2460 (comment) |
I'm submitting a...
Current behavior
Unable to pass multiple objects on one token.
Expected behavior
Providers should support he
multi
boolean that allows you to inject multiple providers.What is the motivation / use case for changing the behavior?
Behavior similar to Angular
The text was updated successfully, but these errors were encountered: