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

fix: grpc-js example #1362

Conversation

reggiemcdonald
Copy link
Contributor

Which problem is this PR solving?

Short description of the changes

  • Recompiled the proto file with grpc-tools. Bellow was the command used:
# Assuming that `helloworld.proto` is at the root of the example
npm install
npx grpc_tools_node_protoc \
  --js_out=import_style=commonjs,binary:./ \
  --grpc_out=grpc_js:./ \
  -I=./ helloworld.proto

grpc_tools_node_protoc provides the protoc interface, but the output is compatible with grpc-js

reggiemcdonald added 2 commits July 28, 2020 17:23
Signed-off-by: reggiemcdonald <regmcdonald@google.com>
Signed-off-by: reggiemcdonald <regmcdonald@google.com>
@reggiemcdonald reggiemcdonald changed the title 1357 fix grpc js example fix: grpc-js example Jul 28, 2020
@reggiemcdonald
Copy link
Contributor Author

/cc @nlehrer @danherr

@codecov
Copy link

codecov bot commented Jul 28, 2020

Codecov Report

Merging #1362 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1362   +/-   ##
=======================================
  Coverage   93.18%   93.18%           
=======================================
  Files         149      149           
  Lines        4228     4228           
  Branches      869      869           
=======================================
  Hits         3940     3940           
  Misses        288      288           

if (!(arg instanceof helloworld_pb.HelloReply)) {
throw new Error('Expected argument of type HelloReply');
throw new Error('Expected argument of type helloworld.HelloReply');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('Expected argument of type helloworld.HelloReply');
throw new Error('Expected argument of type helloworld_pb.HelloReply');

// The greeting service definition.
const GreeterService = (exports.GreeterService = {
var GreeterService = exports.GreeterService = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this a var?

'use strict';
var grpc = require('@grpc/grpc-js');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the changes to this file appear to be unrelated to the purpose of the PR?

Copy link
Contributor Author

@reggiemcdonald reggiemcdonald Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the recompiled output from protoc. Would you prefer me to manually edit the file to make it work? There would be a much smaller diff if I make the edits manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize this was generated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea both the grpc and grpc-js repos use generated code from protoc, using a proto file from a grpc example on github.com/grpc/grpc

const writer = new jspb.BinaryWriter();
this.serializeBinaryToWriter(writer);
proto.helloworld.HelloReply.prototype.serializeBinary = function() {
var writer = new jspb.BinaryWriter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many places where const rewritten to var. Any reason for this?

Copy link
Contributor Author

@reggiemcdonald reggiemcdonald Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the commonjs flag to protoc. Maybe theres a different flag to get let and const

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not overly familiar with protoc so I'm not sure.

@obecny
Copy link
Member

obecny commented Jul 28, 2020

would it be possible to use @grpc/proto-loader instead and then remove the generated files completely ?

@reggiemcdonald
Copy link
Contributor Author

would it be possible to use @grpc/proto-loader instead and then remove the generated files completely ?

The proto file used in this example is from github.com/grpc/grpc. So we would need to commit that file to the repository for the proto-loader option to work.

To keep the diff down, I could edit the existing file manually - you can see what that would look like here. It works, and it's much smaller. It just isn't what the protobuf compiler would output.

@obecny
Copy link
Member

obecny commented Jul 28, 2020

would it be possible to use @grpc/proto-loader instead and then remove the generated files completely ?

The proto file used in this example is from github.com/grpc/grpc. So we would need to commit that file to the repository for the proto-loader option to work.

To keep the diff down, I could edit the existing file manually - you can see what that would look like here. It works, and it's much smaller. It just isn't what the protobuf compiler would output.

I have been in the same place recently when I was adding support for proto for collector exporter. Started with generated files, but it turned out I can simply load the proto files and then use the package without the need to generate the files. Much easier to maintain, less errors and code.

@dyladan
Copy link
Member

dyladan commented Jul 28, 2020

would it be possible to use @grpc/proto-loader instead and then remove the generated files completely ?

The proto file used in this example is from github.com/grpc/grpc. So we would need to commit that file to the repository for the proto-loader option to work.

To keep the diff down, I could edit the existing file manually - you can see what that would look like here. It works, and it's much smaller. It just isn't what the protobuf compiler would output.

I don't think it's an issue to check in the proto file to this example.

The manually edited version also looks ok to me, so for me either is fine.

@markwolff
Copy link
Member

+1 to switching these examples over to protoloading the sample .proto files instead of these codegenerated js files. I ported the plugin tests use .proto + @grpc/proto-loader, so something similar could be done with these samples.

The only reason the old grpc examples had the codegen example at all is because that grpc module supported several different ways of setting up a client. For grpc-js, all are deprecated except for .proto + @grpc/proto-loader.

@reggiemcdonald
Copy link
Contributor Author

reggiemcdonald commented Jul 28, 2020

Why don't I use the manually edited version for this issue to get the example working, and then open a new issue to move both the grpc and grpc-js examples over to protoloader - which I would be happy to work on 🙂

@reggiemcdonald
Copy link
Contributor Author

closing in favour of #1364

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

Successfully merging this pull request may close these issues.

grpc-js example has missing dependency
4 participants