Support grpc-js for the non generate_package_definition output
Hello GRPC node team!
Currently we use the latest grpc-tools and ts-protoc-gen for generating js/ts code for the GRPC models/services.
We use the simple grpc-tools output without the generate_package_definition option. In the same way as provided here https://github.com/grpc/grpc/tree/master/examples/node/static_codegen
import { credentials } from "grpc";
import { MyServiceClient } from "my_service_grpc_pb";
import { CreateUserRequest } from "my_service_pb";
const client = new MyServiceClient("url", credentials.createInsecure());
client.createUser(new CreateUserRequest(), (error, value) => {});
one more example here https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/examples/src/grpc/client.ts
Pros:
- Due to
ts-protoc-gensupports the same output it's easier to use the typescript types. Example - https://github.com/improbable-eng/ts-protoc-gen/blob/master/examples/generated-grpc-node/proto/orphan_grpc_pb.d.ts#L16. So, you don't need cast it manually - we also don't need to use
makeClientConstructoror/andloadPackageDefinitionin this case
Cons:
- this approach requires to use the "grpc" node library which will be deprecated soon
The question is there any plans to support "grpc-js" for this GRPC output option?
Also please correct me if I'm wrong, but it sounds like that PR proto-loader: Add TypeScript generator https://github.com/grpc/grpc-node/pull/1474 doesn't have this feature at all
The grpc-tools library accepts the argument --grpc_js, which should do what you are asking for.
The linked PR for the @grpc/proto-loader library has the --grpcLib option, which can be used for the same effect.
thanks @murgatroid99
The linked PR for the @grpc/proto-loader library has the --grpcLib option, which can be used for the same effect.
Does it mean that @grpc/proto-loader will support the same output https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/examples/src/grpc/proto/book_grpc_pb.d.ts#L78 as grpc-tools and ts-protoc-gen have
No. It's a completely separate library with separate code generation. I just mean that it also has that option to choose which gRPC implementation library to load in the generated code.
got it thanks @murgatroid99
@murgatroid99 I'm sorry, we noticed that https://github.com/grpc/grpc-node/tree/master/packages/grpc-js said that for it usage you need to use generate_package_definition in grpc-tools as a required option. We've tried to run the plugin with the --grpc_js option and output looks mostly the same as we had before the "grpc". Can you please confirm that generate_package_definition option is required or not.
thanks!
No, I wrote that section of the README before the --grpc_js option was added. The --grpc_js option only does something if you don't use the --generate_package_definition option.