Microservice client registration in dynamicaly configured module for nest.js and loading into service by parameter - microservices

I used shared module with dynamic configuration in application.
Shared module contains interceptor and service which contains client configured from shared parameters.
I injected the service into the interceptor by predefined name (importing SharedModule dynamically into SecondAppModule). So client name can have different value. Inside of service I need to know the clients name before client injecting. Right now it is hard coded:
#Injectable()
export class SumClientService {
constructor(#Inject('MATH_SERVICE') private client: ClientProxy) {
console.log('[SumClientService] - created')
}
sumCalculation(row: number[]): Observable<number> {
return this.client.send<number>({ cmd: 'sum' }, row);
}
}
Question
Is there any ways to load service from context by name in case name known at construction time only?
I've detected two ways at list to past name as a parameter into service without corrupting DI managed by nest.js. But I have no idea how to get access to module context for loading service by specified name (the code of idea fragment is below)
#Injectable()
export class SumClientService {
constructor(#Inject('service_name') private name: string) {
console.log('[SumClientService] - created')
}
client: (clientName: string) => ClientProxy = (clientName: string): ClientProxy => // TODO load by clientName real client from `nest.js` context
// ...
}
P.S.
The idea is to use multiple clients in the same application. I considered scenario one client per module for the first time.
git code

There are few options to create microservice clients in dynamic module:
1st option
in module configuration level:
import { Module } from '#nestjs/common';
import { ClientModule } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
#Module({
imports: [
ClientModule.register([
{ 'NAME_FOR_DI', ...configForYourClient }
])
],
...
})
...
usage:
import { Inject } from '#nestjs/common';
import { ClientProxy } from '#nestjs/microservices';
...
constructor(#Inject('NAME_FOR_DI') private readonly client: ClientProxy) { }
2st option
For dynamically configured module:
import { Module, DynamicModule } from '#nestjs/common';
import { ClientModule } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
#Module({})
export class YourModule{
static register(): DynamicModule {
return {
module: YourModule,
imports: [
ClientModule.register([
{ 'NAME_FOR_DI', ...configForYourClient }
])
],
}
}
}
3st option
import { ClientProxy, ClientProxyFactory } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
...
private client: ClientProxy = ClientProxyFactory.create(configForYourClient)
Following second option it can be created in service directly or registered for module specifying DI key in providers, like:
import { Module } from '#nestjs/common';
import { ClientProxyFactory } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
#Module({
providers: [
{
provide: 'NAME_FOR_DI',
useValue: ClientProxyFactory.create(configForYourClient)
},
],
...
})
...
with following injection by specified key.
P.S.
I've described here general idea for dynamic injection. This idea can be enriched with different combinations (injecting list of values for the single provider, or injecting list of configurations into ClientModule.register where you should apply some corrections in you DI approach)

Related

Nestjs / GraphQL - Playground Returns Null Error For Query. My Resolvers?

Playground in my browser shows the Nestjs created schema nicely but queries are returning null. Is there something wrong with my code?
"errors": [
{
"message": "Cannot return null for non-nullable field Query.getUsers.",
"locations": [
{
"line": 2,
"column": 3
}
This means no data found.
schema.graphql:
type UsersGQL {
User_id: ID!
first_name: String!
last_name: String!
main_skill_title: String!
user_name: String!
....
}
type Query {
getUser(user_id: ID!): UsersGQL!
getUsers: [UsersGQL!]!
}
Compiles in Nestjs with GraphQL to graphql.schema.ts
export class UsersGQL {
user_id: string;
first_name: string;
last_name: string;
main_skill_title: string;
user_name: string;
...
}
export abstract class IQuery {
abstract getUser(user_id: string): UsersGQL | Promise<UsersGQL>;
abstract getUsers(): UsersGQL[] | Promise<UsersGQL[]>;
abstract temp__(): boolean | Promise<boolean>;
}
users.resolvers.ts
import { Query, Resolver } from '#nestjs/graphql';
import { UsersService } from './users.service';
import { UsersGQL } from '../graphql.schema';
// import { UsersDTO } from './users.dto';
#Resolver('UsersGQL')
export class UsersResolvers {
constructor(
private readonly userService: UsersService
) {}
#Query()
async getUsers() {
return await this.userService.findAll();
}
}
The service works fine for my Nestjs REST API's. The db is Postgres.
users.service.ts
import { Injectable } from '#nestjs/common';
import { InjectRepository } from '#nestjs/typeorm';
import { Repository, getManager, getRepository } from 'typeorm';
import { Members } from './members.entity';
#Injectable()
export class UsersService {
private entityManager = getManager();
constructor(
#InjectRepository(Users)
private readonly usersRepository: Repository<Users>
) {}
async findAll(): Promise<Users[]> {
return await this.usersRepository.find();
}
}
Playground query:
{
getUsers {
first_name
last_name
}
}
The error returned in Playground:
{
"errors": [
{
"message": "Cannot return null for non-nullable field Query.getUsers.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getUsers"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
...
],
"data": null
}
Edit - added users.module.ts, app.module.ts and ormconfig.json. This whole module is lazy loaded. REST and GraphQL are side by side in the module. I also separated REST and GQL components.
users.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
// REST
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { Users } from './users.entity';
// GraphQL
import { UsersResolvers } from './users.resolvers';
#Module({
imports: [
TypeOrmModule.forFeature([
Users
]),
],
providers: [
UsersService,
UsersResolvers
],
controllers: [UsersController],
})
export class UsersModule {}
app.module.ts
import { Module, NestModule, MiddlewareConsumer } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { GraphQLModule } from '#nestjs/graphql';
import { join } from 'path';
import { LoggerMiddleware } from './logger.middleware';
import { UsersModule } from './users/users.module';
import { UsersController } from './users/users.controller';
#Module({
imports: [
TypeOrmModule.forRoot(),
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
definitions: {
path: join(process.cwd(), 'src/graphql.schema.ts'),
outputAs: 'class',
},
debug: true,
}),
UsersModule
],
controllers: [
],
exports: [
],
providers: [
]
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(LoggerMiddleware)
.with('AppModule')
.forRoutes(
UsersController
)};
}
ormconfig.json
...
"entities": [
"src/**/**.entity{.ts,.js}",
// "src/graphql.schema.ts" This doesn't work. Must use REST entity.
],
...
You probably imported #Query from '#nestjs/common' instead of '#nestjs/graphql'.
Make sure to have:
import { Query } from '#nestjs/graphql';
TL;DR
The fact that your schema is correctly exposed (and available via the playground) doesn't necessarily mean that all the corresponding modules and resolvers are integrated into your running Nest application.
I recently faced the same error and the reason was quite straightforward: I had forgotten to import the module including the resolver into the root module — usually AppModule.
So: are you sure you have all your UserModule module dependencies (and above all the UserResolver) imported and the UsersModule itself imported into your AppModule?
The trick here is that the GraphQL schema exposed by your server is directly generated from your source files. According to your Nest GraphQL configuration, it will compile all the .graphql, .gql files together (schema first approach) ; or the type-graphql module, with Nest v6, will read all your source files looking for its decorators and generate the schema.gql (code first approach). As a consequence, you can expose a correct schema even having no actual module resolving your request.
IMO, it's a buggy behaviour from the framework as it silently fails to resolve your schema without providing any explanation. The simple error message you get (Cannot return null for non-nullable field Query.getUsers.) is quite misleading as it hides the real failure, which is a broken dependency.
For more information, here is the related GitHub issue: https://github.com/nestjs/graphql/issues/198
The "solution" with this TypeORM architecture is to use the TypeORM entity.
users.resolvers.ts
import { Query, Resolver } from '#nestjs/graphql';
import { UsersService } from './users.service';
import { Users } from './users.entity'; // Here
#Resolver('Users')
export class UsersResolvers {
constructor(
private readonly userService: UsersService
) {}
#Query()
async getUsers() {
return await this.userService.findAll();
}
}

How to access websocket from controller or another component/services?

I have a REST API, I want to send event to the client via websocket.
How to inject websocket instance in controller or another component?
Better solution is to create global module. You can then emit events from any other module/controller. A. Afir approach will create multiple instances of Gateway if you try to use it in other modules.
Note: This is just simplest solution
Create socket.module.ts
import { Module, Global } from '#nestjs/common';
import { SocketService } from './socket.service';
#Global()
#Module({
controllers: [],
providers: [SocketService],
exports: [SocketService],
})
export class SocketModule {}
socket.service.ts
import { Injectable } from '#nestjs/common';
import { Server } from 'socket.io';
#Injectable()
export class SocketService {
public socket: Server = null;
}
app.gateway.ts see afterInit function
import { WebSocketGateway, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, WebSocketServer } from '#nestjs/websockets';
import { Logger } from '#nestjs/common';
import { Server, Socket } from 'socket.io';
import { SocketService } from './socket/socket.service';
#WebSocketGateway()
export class AppGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(private socketService: SocketService){
}
#WebSocketServer() public server: Server;
private logger: Logger = new Logger('AppGateway');
afterInit(server: Server) {
this.socketService.socket = server;
}
handleDisconnect(client: Socket) {
this.logger.log(`Client disconnected: ${client.id}`);
}
handleConnection(client: Socket, ...args: any[]) {
this.logger.log(`Client connected: ${client.id}`);
}
}
Then import SocketModule into AppModule and you can use Socket service everywhere.
class Gateway can be injected in another component, and use the server instance.
#Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly messageGateway: MessageGateway
) {}
#Get()
async getHello() {
this.messageGateway.server.emit('messages', 'Hello from REST API');
return this.appService.getHello();
}
}
I suppose that #Raold missed a fact in the documentation:
Gateways should not use request-scoped providers because they must act as singletons. Each gateway encapsulates a real socket and cannot be instantiated multiple times.
So it means that we can neither instantiate the gateway class multiple times nor do it explicitly using injection scopes features.
So creating just only one gateway for one namespaces will be right and it will produce only one instance of the websocket or socket.io server.

Angular SharedService with BehaviorSubject lost Data on refresh

i created sharedService it works perfectly , i can shared data from one component to another (this both are irrelevant component in different module).
Data Transfer as follows:
AdminDashboard.Component (update value) ===> conference.component (get new updated value)
problem : when i refresh my conference.component i lost the value
EventService.ts
import { Injectable } from '#angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { importExpr } from '#angular/compiler/src/output/output_ast';
import {Events} from '../models/event.model'
#Injectable()
export class EventService {
private dataSource = new BehaviorSubject(null);
sendMessage(data) {
this.dataSource.next(data);
}
getMessage(): Observable<any> {
return this.dataSource.asObservable();
}
}
dashboard.component (url /dashboard)
on Button Click msg() method called , which updated BehaviourSubjectvalue.
import { Component, OnInit} from '#angular/core';
import { NgForm } from '#angular/forms';
import { EventService } from '../../shared/sharedServies/eventService.service';
export class AdminDashboardComponent implements OnInit {
constructor( private testEventService: EventService){ }
msg() {
debugger
this.testEventService.sendMessage('Message from Home Component to App
Component!');
}
}
conference.component (url /conference)
Here , i hold value in message and bind to ui.
import { Component, OnInit } from '#angular/core';
import { EventService } from'../../shared/sharedServies/eventService.service';
import { Subscription } from 'rxjs/Subscription';
export class ViewconferenceComponent implements OnInit {
message: any;
constructor(private EventService: EventService) {
this.subscription = this.EventService.getMessage().subscribe(message => {
console.log(message)
this.message = message;
});
}
}
Question :
when i get data on /conference page , at this when i refresh the
service holded value is lost , i didn't understand what this happens.
also i need to add json to sharedService , how it will achive?
This is expected since when you "switch" components they are destroyed. You could work around this quickly by adding state variables to your service.
Personally, I encourage you to make use of some state library like ngRx https://github.com/ngrx/platform

'object%20Object' Being Appended instead of parameters.

I am attempting to make a call to the server using promises. When trying to add my parameters, it comes out as 'object%20Object'
Here is the call
import { Injectable } from '#angular/core';
import { Http } from '#angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import { User } from '../models/user';
#Injectable()
export class UserService {
private baseUserUrl = 'api/User/'
constructor(private http: Http) { }
getUsers(currentPage: number): Promise<User[]> {
return this.http.get(this.baseUserUrl + 'GetUsers?currentPage=' + currentPage)
.map(resp => resp.json() as User[])
.toPromise()
}
}
I was accidentally passing an object into the method, so I wasn't accessing the property, I was accessing the object. I fixed that and removed the object and passed a property.

Not Able To use simple service in the angular 2 component, getting compilation errors visual studio

<!--Simple Angular2 Service-->
import { Injectable } from '#angular/core';
#Injectable()
export class HomeService {
constructor() {
}
getSomething() {
return 'hiii';
}
}
<!--Consuming Service in Component-->
import { Component } from '#angular/core';
import { HomeService } from './components/home/home.service';
#Component({
selector:'home',
templateUrl:'app/components/home/home.html'
})
export class HomeComponent {
}
<!--I am Getting below compilation Errors-->
Error TS2307 Cannot find module 'app/components/home/home.service'.
Error Build:Cannot find module './components/home/home.service'.
Please help stuck here for 3 days

Resources