Apologies for the lengthy title...
I am in the process of developing a basic crud system using gRPC and typescript. My issue lies in the fact that the auto-generated file creates a class and a type for each parameter in my protoFile
. For example, the UserId parameter generates a class with getUserId, and a namespace with the type for UserId.
The problem arises when trying to use the method in my client
, as typescript expects a class as a parameter instead of the type.
So instead of getUsersById({id: 1}, callback)
... it prompts me to use getUsersById(new UserId)
.
user.Proto:
syntax = "proto3";
package userServicePKG;
message User {
int32 id = 1;
string name = 2;
int32 age = 3;
}
message UserId {
int32 id = 1;
}
service UserService{
rpc getUserById (UserId) returns (User);
}
UserServiceClientPb.ts (Protobuf generated) - Functions definition
methodInfogetUserById = new grpcWeb.AbstractClientBase.MethodInfo(
User,
(request: UserId) => {
return request.serializeBinary();
},
User.deserializeBinary
);
getUserById(
request: UserId,
metadata: grpcWeb.Metadata | null): Promise<User>;
getUserById(
request: UserId,
metadata: grpcWeb.Metadata | null,
callback: (err: grpcWeb.Error,
response: User) => void): grpcWeb.ClientReadableStream<User>;
getUserById(
request: UserId,
metadata: grpcWeb.Metadata | null,
callback?: (err: grpcWeb.Error,
response: User) => void) {
if (callback !== undefined) {
return this.client_.rpcCall(
new URL('/userServicePKG.UserService/getUserById', this.hostname_).toString(),
request,
metadata || {},
this.methodInfogetUserById,
callback);
}
return this.client_.unaryCall(
this.hostname_ +
'/userServicePKG.UserService/getUserById',
request,
metadata || {},
this.methodInfogetUserById);
}
user_pb.d.ts (Protobuf Generated) - Define types and classes:
export class UserId extends jspb.Message {
getId(): number;
setId(value: number): UserId;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): UserId.AsObject;
static toObject(includeInstance: boolean, msg: UserId): UserId.AsObject;
static serializeBinaryToWriter(message: UserId, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): UserId;
static deserializeBinaryFromReader(message: UserId, reader: jspb.BinaryReader): UserId;
}
export namespace UserId {
export type AsObject = {
id: number,
}
}
Client.Vue:
const client = new UserServiceClient('http://localhost:5001', null, null);
let userId = { id:1 };
client.getUserById(userId, function (error: grpcWeb.Error, response: any) {
//do something
});
The parameter userId triggers the following error:
Argument of type '{ id: number; }' is not assignable to parameter of type 'UserId'. Type '{ id: number; }' is missing the following properties from type 'UserId': getId, setId, serializeBinary, toObject, and 8 more.Vetur(2345)
It appears that typescript is interpreting that getUserById
first parameter is of the type Class UserId
rather than the type originating from the namespace UserId
.
Is there a solution to this issue? Since it was auto-generated, shouldn't it interpret correctly? Could I be making a mistake elsewhere? I am relatively new to gRPC so I may have misunderstood something.
Thank you in advance!