grpc-node icon indicating copy to clipboard operation
grpc-node copied to clipboard

Support grpc-js for the non generate_package_definition output

Open andriictkn opened this issue 6 years ago • 6 comments

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-gen supports 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 makeClientConstructor or/and loadPackageDefinition in 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

andriictkn avatar Aug 12 '20 21:08 andriictkn

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.

murgatroid99 avatar Aug 12 '20 21:08 murgatroid99

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

andriictkn avatar Aug 12 '20 21:08 andriictkn

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.

murgatroid99 avatar Aug 12 '20 22:08 murgatroid99

got it thanks @murgatroid99

andriictkn avatar Aug 12 '20 22:08 andriictkn

@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!

andriictkn avatar Aug 12 '20 22:08 andriictkn

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.

murgatroid99 avatar Aug 13 '20 16:08 murgatroid99