Nest: Support multi option for providers

Created on 7 Jun 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

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

core type todo 馃挌

Most helpful comment

Please include this! It looks like a lot of the work has already be done? @BrunnerLivio

All 4 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.

@Module({
  providers: [
    {
      provide: 'TEST',
      useClass: Test1,
      multi: true,
      priority: 0, // default
    },
    {
      provide: 'TEST',
      useClass: Test2,
      multi: true,
      priority: 1,
    }],
})

So now, @Inject('TEST') injects an array of [Test2, Test1].

Symfony documentation:
https://symfony.com/doc/current/service_container/tags.html#reference-tagged-services

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 {}
Was this page helpful?
0 / 5 - 0 ratings