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

Broken typescript exports after moving to @grpc/grpc-js #1600

Closed
ikibalnyi opened this issue Oct 7, 2020 · 5 comments
Closed

Broken typescript exports after moving to @grpc/grpc-js #1600

ikibalnyi opened this issue Oct 7, 2020 · 5 comments

Comments

@ikibalnyi
Copy link

Hey,

After updating to @grpc/grpc-js all the exports got changed from export BookService

out->Print(template_vars, "var $name$Service = exports.$name$Service = {\n");
to export['com.book.BookService'].
out->Print(template_vars, "var $name$Service = exports['$full_name$'] = {\n");

What is the point of such change? Now it's not possible to generate proper typescript definitions for such exports.

// grpc
import { BookServiceClient } from "./proto/book_grpc_pb";
const client = new BookServiceClient("127.0.0.1:50051", grpc.credentials.createInsecure());

// changed to

// grpc-js
import * as bookGrpcPb from "./proto/book_grpc_pb";
// @ts-ignore
const BookServiceClient = grpc.makeClientConstructor(bookGrpcPb["com.book.BookService"], "BookService");

So now such export is not valid for Typescript and you have to write // @ts-ignore.
Why was removed export of the BookServiceClient? or at least BookService?

@murgatroid99
Copy link
Member

That second line you linked is in the generate_package_definition branch, which is triggered by using the generate_package_definition code generation option. That option makes the generated file export an object that is intended to be loaded using grpc.loadPackageDefinition. You could then apply a TypeScript type to the resulting object.

However, using that option is not the only way to generate code for use with the grpc-js library. You could also use the grpc_js code generation option instead. That will generate code very similar to the code generated without any options, except that it will import @grpc/grpc-js instead of grpc.

@ikibalnyi
Copy link
Author

@murgatroid99 that helped, thank you!

Fixed by changing:

--grpc_out=generate_package_definition:$OUT_DIR

to

--grpc_out=grpc_js:$OUT_DIR

@agreatfool
Copy link

agreatfool commented Oct 9, 2020

One more question, from which version of grpc-tools the flag grpc_js get available?

@ikibalnyi
Copy link
Author

From what I've found it's available starting from 1.9.0 https://github.com/grpc/grpc-node/releases/tag/grpc-tools%401.9.0

@agreatfool
Copy link

@ikibalnyi OK, thanks. Then I could give the upgrade warning. Next version of ts grpc tool have to be upgraded with grpc-tools version over 1.9.0.

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

3 participants