Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @grpc/proto-loader serialization and promises #35

Open
tatemz opened this issue Jan 7, 2019 · 4 comments
Open

Support @grpc/proto-loader serialization and promises #35

tatemz opened this issue Jan 7, 2019 · 4 comments

Comments

@tatemz
Copy link

tatemz commented Jan 7, 2019

The @grpc/proto-loader package allows for on-demand loading of .proto files in order to dynamically create a node server or client. There is a pretty good example here:

https://github.com/grpc/grpc/tree/v1.17.1/examples/node/dynamic_codegen

A few things are different about the way proto-loader generates the method definitions. Instead of using request/response objects with getters and setters, the method definitions use plain objects. The typings generated usually include an AsObject version of the request/response objects, however, the client definitions generated by your plugin do not match the client implementation created by proto-loader

The following example is the definitions generated by your plugin:

export interface IGreeterClient {
    greet(request: greeter_pb.GreetingRequest, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
}

export class GreeterClient extends grpc.Client implements IGreeterClient {
    constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
    public greet(request: greeter_pb.GreetingRequest, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse) => void): grpc.ClientUnaryCall;
}

The following is what it should look like if one was using proto-loader:

export interface IGreeterClient {
    greet(request: greeter_pb.GreetingRequest.AsObject, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
}

export class GreeterClient extends grpc.Client implements IGreeterClient {
    constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
    public greet(request: greeter_pb.GreetingRequest.AsObject, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
    public greet(request: greeter_pb.GreetingRequest.AsObject, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: greeter_pb.GreetingResponse.AsObject) => void): grpc.ClientUnaryCall;
}

Also not included in my above example is the support for Promises. In general your examples probably need to be updated to match the vanilla JS ones provided in the sample link above.

@agreatfool
Copy link
Owner

Hi @tatemz,

Thank you for your information. I just knew this trend in your issue.

And I have several questions:

  • Is there any roadmap of project proto-loader?
  • Is there any discussion group or mail list of project proto-loader?
  • Where did you find the code generated by proto-loader? (as the second block of codes in your issue description) I just read the repo & the example repo you provided, and did't find how to generate static codes.

@tatemz
Copy link
Author

tatemz commented Jan 16, 2019

The official grpc-node project provides the proto-loader package.
https://github.com/grpc/grpc-node/tree/master/packages/proto-loader

  • Is there any roadmap of project proto-loader?
    • Not that I know of apart from what little Google provides here
  • Is there any discussion group or mail list of project proto-loader?
    • Haven't seen one yet, but I bet there is a Google Group somewhere 🤷‍♂️
  • Where did you find the code generated by proto-loader?
    • I was just guessing the types based on how the "promisified" version would look like

@tatemz
Copy link
Author

tatemz commented Jan 16, 2019

proto-loader uses protobuf.js behind the scenes and there is a lot of documentation there with further details

@tatemz
Copy link
Author

tatemz commented Jan 16, 2019

On further investigation, it seems as if protobuf.js supplies Typescript definitions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants