|
| 1 | +# @grpc/grpc-js server implementation signature issue |
| 2 | + |
| 3 | +## Status |
| 4 | +Issue: [Issue#79](https://github.com/agreatfool/grpc_tools_node_protoc_ts/issues/79). |
| 5 | + |
| 6 | +This one is fixed in version `v5.1.0`, and there should be some document to explain more. |
| 7 | + |
| 8 | +## Issue |
| 9 | +If you are using this tool of version `v5.0.1`, you may have already noticed a signature issue here: [examples/src/grpcjs/server.ts#L74-L75](https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/v5.0.1/examples/src/grpcjs/server.ts#L74-L75). Note the `// @ts-ignore`: |
| 10 | + |
| 11 | +```typescript |
| 12 | +// @ts-ignore |
| 13 | +server.addService(BookServiceService, new ServerImpl()); |
| 14 | +``` |
| 15 | + |
| 16 | +The signature of implementation class looks like: |
| 17 | + |
| 18 | +```typescript |
| 19 | +export const BookServiceService: IBookServiceService; |
| 20 | + |
| 21 | +export interface IBookServiceServer { |
| 22 | + getBook: grpc.handleUnaryCall<book_pb.GetBookRequest, book_pb.Book>; |
| 23 | + getBooksViaAuthor: grpc.handleServerStreamingCall<book_pb.GetBookViaAuthor, book_pb.Book>; |
| 24 | + getGreatestBook: handleClientStreamingCall<book_pb.GetBookRequest, book_pb.Book>; |
| 25 | + getBooks: grpc.handleBidiStreamingCall<book_pb.GetBookRequest, book_pb.Book>; |
| 26 | +} |
| 27 | + |
| 28 | +class ServerImpl implements IBookServiceServer { |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +The signature of `addService` is: |
| 33 | + |
| 34 | +```typescript |
| 35 | +export class Server { |
| 36 | + addService( |
| 37 | + service: ServiceDefinition, |
| 38 | + implementation: UntypedServiceImplementation |
| 39 | + ): void {} |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +As you can see the type of implementation need to be `UntypedServiceImplementation`. So it's inconsistent. |
| 44 | + |
| 45 | +## Fixing |
| 46 | +The way of fixing is to define IBookServiceServer to extends `UntypedServiceImplementation`. |
| 47 | + |
| 48 | +```typescript |
| 49 | +export interface IBookServiceServer extends grpc.UntypedServiceImplementation { |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Though this fixing solved the issue of signature issue here (the original one): |
| 54 | + |
| 55 | +```typescript |
| 56 | +// @ts-ignore |
| 57 | +server.addService(BookServiceService, new ServerImpl()); |
| 58 | +``` |
| 59 | + |
| 60 | +It brings the new issue. |
| 61 | + |
| 62 | +Because `UntypedServiceImplementation` is defined like: |
| 63 | + |
| 64 | +```typescript |
| 65 | +export interface UntypedServiceImplementation { |
| 66 | + [name: string]: UntypedHandleCall; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +This means all the attributes inside the `UntypedServiceImplementation` (and any derived implementation, like `class ServerImpl implements IBookServiceServer extends grpc.UntypedServiceImplementation`) have to be type of `UntypedHandleCall`. |
| 71 | + |
| 72 | +So when you define a class to implement `IBookServiceServer`, you have to add a line of code, like: |
| 73 | + |
| 74 | +```typescript |
| 75 | +class Impl implements IBookServiceServer { |
| 76 | + [name: string]: grpc.UntypedHandleCall; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Otherwise, tsc would throw errors: [Typescript: Index signature is missing in type](https://stackoverflow.com/questions/37006008/typescript-index-signature-is-missing-in-type). |
| 81 | + |
| 82 | +## Implementation |
| 83 | +According to this breaking change, there could be two styles of server side implementation. One is `Object style`, the other is `Class style`. |
| 84 | + |
| 85 | +### Object Style |
| 86 | + |
| 87 | +```typescript |
| 88 | +const Impl: IBookServiceServer = { |
| 89 | + getBook: (call: grpc.ServerUnaryCall<GetBookRequest, Book>, callback: sendUnaryData<Book>): void => {}, |
| 90 | + |
| 91 | + getBooks: (call: grpc.ServerDuplexStream<GetBookRequest, Book>): void => {}, |
| 92 | + |
| 93 | + getBooksViaAuthor: (call: grpc.ServerWritableStream<GetBookViaAuthor, Book>): void => {}, |
| 94 | + |
| 95 | + getGreatestBook: (call: grpc.ServerReadableStream<GetBookRequest, Book>, callback: sendUnaryData<Book>): void => {}, |
| 96 | +}; |
| 97 | + |
| 98 | +const server = new grpc.Server(); |
| 99 | +server.addService(BookServiceService, Impl); |
| 100 | +``` |
| 101 | + |
| 102 | +This style is `recommended`. Since you can append more attributes in the `Impl` object, though they are not defined in `IBookServiceServer`, and you don't need to add `[name: string]: grpc.UntypedHandleCall` in your codes. |
| 103 | + |
| 104 | +### Class Style |
| 105 | + |
| 106 | +```typescript |
| 107 | +class Impl implements IBookServiceServer { |
| 108 | + [name: string]: grpc.UntypedHandleCall; |
| 109 | + |
| 110 | + public getBook(call: grpc.ServerUnaryCall<GetBookRequest, Book>, callback: sendUnaryData<Book>): void {} |
| 111 | + |
| 112 | + public getBooks(call: grpc.ServerDuplexStream<GetBookRequest, Book>) {} |
| 113 | + |
| 114 | + public getBooksViaAuthor(call: grpc.ServerWritableStream<GetBookViaAuthor, Book>) {} |
| 115 | + |
| 116 | + public getGreatestBook(call: grpc.ServerReadableStream<GetBookRequest, Book>, callback: sendUnaryData<Book>) {} |
| 117 | +} |
| 118 | + |
| 119 | +const server = new grpc.Server(); |
| 120 | +server.addService(BookServiceService, new Impl()); |
| 121 | +``` |
| 122 | + |
| 123 | +This style is `NOT` recommended. Since only those already defined in the `IBookServiceServer` can be existing in this `Impl` class, and `[name: string]: grpc.UntypedHandleCall` is required for Class style. |
0 commit comments