Reputation: 151
I am learning something new with NestJS and gRPC. I'm just running into a problem. I have created an auth microservice and a gateway but I get an empty response back.
The gateway and the microservice are triggered. but I get an empty object back. If I do hardcode in the gateway without gRPC then it works. Does anyone know what I've forgotten?
Gateway
import { Body, Controller, OnModuleInit, Post } from '@nestjs/common';
import { Client, ClientGrpc } from '@nestjs/microservices';
import { authOptions } from 'src/grpc.options';
import { AuthService } from './grpc.interface';
@Controller('auth')
export class AuthController implements OnModuleInit {
@Client(authOptions)
private client: ClientGrpc;
private authService: AuthService;
onModuleInit() {
this.authService = this.client.getService<AuthService>('AuthService');
}
@Post('login')
async login(@Body('username') username: string, @Body('password') password: string)
{
return this.authService.login({ username, password });
}
}
Auth Microservice
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
interface LoginArgs {
username: string;
password: string;
}
interface LoginReturn {
access_token: string;
}
@Controller('auth')
export class AuthController {
@GrpcMethod('AuthService', 'Login')
login(data: LoginArgs, metadata: any): LoginReturn
{
return {
access_token: '123',
}
}
}
Proto
syntax = "proto3";
package auth;
service AuthService {
rpc Login (LoginArgs) returns (LoginReturn);
}
message LoginArgs {
string username = 1;
string password = 2;
}
message LoginReturn {
string access_token = 1;
}
Upvotes: 2
Views: 1524
Reputation: 301
gRPC Client does not send fields when field name contains underscore.
You should delete all underscores from access_token and then works.
Or add the following keepCase: true to the connection option (as here mentioned)
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.GRPC,
options: {
package: 'test',
protoPath: 'path/to/proto',
loader: { keepCase: true },
},
});
Upvotes: 4