#can't inject dependency that I'm registering as a provider in the same module?

28 messages · Page 1 of 1 (latest)

pure cipher
#

I have an "eventsAppModule" which is a core application, in its providers array, I provide:

      provide: PUB_SUB,```

And then in the events resolver that's registered as a provider in the array of the same module, like this: 

```@Resolver()
export class EventsResolver {
  constructor(
    @Inject(PUB_SUB) private pubSub: RedisPubSub,``` but it throws that it can't resolve that PUB_SUB dependency, any ideas why?
#

Following this article that seems to link to an SO issue by @sweet obsidian but then in that issue it's about useclass not useFactory

#
@Module({
  imports: [YeoSecretsModule],
  providers: [
    {
      provide: PUB_SUB,
      useFactory: (secretsService: YeoSecretsService) => {
        const auth = secretsService.get('REDIS_AUTH').split(':');
        return new RedisPubSub({
          connection: {
            host: auth[0],
            port: parseInt(auth[1]),
            password: auth[2],
          },
        });
      },
      inject: [YeoSecretsService],
    },
  ],
  exports: [PUB_SUB],
})
export class PubSubModule {}``` tried with a completely separate module, no luck
sweet obsidian
#

What's the error you're getting?

pure cipher
#

classic

#

Nest can't resolve dependencies of the EventsResolver (?, YeoTracerService). Please make sure that the argument dependency at index [0] is available in the EventServiceAppModule context.

Potential solutions:

  • If dependency is a provider, is it part of the current EventServiceAppModule?
  • If dependency is exported from a separate @Module, is that module imported within EventServiceAppModule?
    @Module({
    imports: [ /* the Module containing dependency */ ]
    })
sweet obsidian
#

Seems like a circumstances import issue

#

Where do you define the PUB_SUB constant?

pure cipher
#

export const PUB_SUB = 'PUB_SUB'; inside event-service.app.module

sweet obsidian
#

Yeah, move that to a different file

pure cipher
#

huh. What if I just put in a string "PUBSUB" instead of a const?

#

i'm obviously trrying it right now

#

app seems to start hmmm, without the const just fine

sweet obsidian
#

Yeah. The issue is that it's currently coordinating imports which can lead to the constant being evaluated as undefined

pure cipher
#

awesome, unrelated issue, but can't seem to subscribe, saying 'cannot return nulll for non-nullable field Subscription.newMessage"

#
  newMessage() {
    return this.pubSub.asyncIterator('newMessage');
  }```
#
export class NewMessageEvent {
  @Field({ nullable: true })
  message?: string;
}
#
      console.log('NEW MESSAGE', data);
    });```
#

so I can see that the output of that is truly an object with message: string -- the right type

#
    resolve: (value) => {
      console.log('RETURNING VALUE', value);

      return value ?? { message: 'UNDEFINED' };
    },
  })```
#

yep it seems that async iterator is returning undefined

#

ah this is an issue with graphql subscriptions

#
          debug: true,
          installSubscriptionHandlers: true,
          playground: true,
          autoSchemaFile: './apps/event-service/schema.gql',
          sortSchema: true,
          context: ({ req, res }) => ({ req, res }),
        };``` did this part -- I used to have graphql-ws under 'subscriptions' but then it complains that it can't connect
#

I'm gonna re-read the docs on graphql subscriptions aaah

#

alright I have "graphql-ws": "5.11.2", and as per the docs I am configuring it solely as subscriptions: { 'graphql-ws': true, },

#

made a completely separate post about that issue