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

Add Server interface #247

Merged
merged 6 commits into from Dec 3, 2020

Conversation

badsyntax
Copy link
Contributor

@badsyntax badsyntax commented Sep 23, 2020

Unless i'm completely missing something, it's not possible to use the types generated by this package for a server implementation. This change fixes that.

I've had to make various upstream changes to allow the types to work across the grpc and @grpc/grpc-js packages. I've been following & contributing to the proto-loader typescript generator changes in grpc/grpc-node#1474, and changes in this PR align with those changes.

If using @grpc/grpc-js, a minimum version of 1.2.0 is required. I'm not sure how best to handle this, whether we would mention this in the readme, or add it as a peerDependency?

  • This PR is a draft because there's a mismatch of types between the grpc and @grpc/grpc-js packages. The code in this PR will generate types that only work with @grpc/grpc-js. I hope to get come clarity on the types mismatch soon.
  • UPDATE: Waiting on the latest type changes to @grpc/grpc-js to be published, to support this change.
  • UPDATE 2: We still need the types to match in grpc for this to work.

Changes

  • Generate a server interface

Example usage

import * as grpc from '@grpc/grpc-js';
import { ChatService, IChatServer } from './proto/chat_grpc_pb';
import { ClientMessage, ServerMessage } from './proto/chat_pb';

const chatServer: IChatServer = {
  join(call: grpc.ServerWritableStream<ClientMessage, ServerMessage>): void {
    // implementation
  },
  send(
    call: grpc.ServerUnaryCall<ClientMessage, ServerMessage>,
    callback: grpc.sendUnaryData<ServerMessage>
  ): void {
    // implementation
  },
};

const server = new grpc.Server();
server.addService(ChatService, chatServer);

Verification

I've used the generated files in an example app and they seem ok. I briefly explored writing tests but all the existing tests are testing the javascript implementation and not the generated types, so not sure how best to create tests for typescript interfaces.

I believe we can ignore the the failing build (linting) as this new code is consistent with existing code.

Related

#221

src/service/grpcnode.ts Outdated Show resolved Hide resolved
This approach provides between type compatibility between grpc and grpc-js packages
@IchordeDionysos
Copy link

@badsyntax what's the status of this PR? 🤔

@badsyntax
Copy link
Contributor Author

@IchordeDionysos unfortunately i'm still waiting on these changes to be released:

grpc/grpc-node#1587
grpc/grpc-node#1590

i need those changes to build a generic service interface that will work with both grpc and @grpc/grpc-js packages

@badsyntax
Copy link
Contributor Author

badsyntax commented Nov 16, 2020

grpc/grpc-node#1587 is published so this PR is good for a review!

Any thoughts about this @MarcusLongmuir?

@badsyntax badsyntax marked this pull request as ready for review November 16, 2020 20:42
@badsyntax
Copy link
Contributor Author

@MarcusLongmuir Any thoughts about this one? It's not a huge change but would be helpful for people i reckon.

Copy link
Contributor

@MarcusLongmuir MarcusLongmuir left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Minor note about the dependency, but otherwise LGTM 👍

package.json Outdated
@@ -33,6 +33,7 @@
"google-protobuf": "^3.6.1"
},
"devDependencies": {
"@grpc/grpc-js": "^1.2.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably best expressed in the README rather than this package specifying it as a dev dependency as that might not get noticed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think adding it as a dev dependency was intended to make it easier to test the generated types, but it's not required by the package so I have remove it.
I've also added a compatibility note to the README.

@improbable-prow-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: MarcusLongmuir
To complete the pull request process, please assign
You can assign the PR to them by writing /assign in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@MarcusLongmuir MarcusLongmuir merged commit 7b8d3ca into improbable-eng:master Dec 3, 2020
@badsyntax
Copy link
Contributor Author

Thanks for the merge @MarcusLongmuir. Is there an ETA for a release?

@MarcusLongmuir
Copy link
Contributor

Apologies for the delay. We've just been overhauling a lot of permissions and repo configurations that made this rather laborious.

This has gone out in 0.14.0: https://www.npmjs.com/package/ts-protoc-gen/v/0.14.0

@badsyntax
Copy link
Contributor Author

Awesome thank you!

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.

None yet

4 participants