ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

Support outputting only client code for grpc-js

Open akornatskyy opened this issue 2 years ago • 7 comments

With the following options:

env=node,outputServices=grpc-js,esModuleInterop=true,outputJsonMethods=false,outputPartialMethods=false,outputServices=false

and proto3 file:

syntax = "proto3";

package runtime.v1;

service RuntimeService {
    rpc Version(VersionRequest) returns (VersionResponse) {}
}

message VersionRequest {
    string version = 1;
}

message VersionResponse {
    string version = 1;
    string runtime_name = 2;
    string runtime_version = 3;
    string runtime_api_version = 4;
}

Unused RuntimeServiceServer interface and imports generated:

import {
  handleUnaryCall,
  UntypedServiceImplementation,
} from "@grpc/grpc-js";

export interface RuntimeServiceServer extends UntypedServiceImplementation {
  version: handleUnaryCall<VersionRequest, VersionResponse>;
}

akornatskyy avatar Feb 08 '23 08:02 akornatskyy

You've got outputServices twice in your config param, so it ends up being:

        "outputServices": Array [
          "grpc-js",
          false,
        ],

Just remove the outputServices=grpc-js since you don't want them?

stephenh avatar Feb 09 '23 13:02 stephenh

I have removed outputServices=false, but RuntimeServiceServer is still there and not used (anywhere in generated code).

Is there a way to split client / server parts?

akornatskyy avatar Feb 09 '23 14:02 akornatskyy

Oh, I see, outputServices=grpc-js calls this:

export function generateGrpcJsService(
  ctx: Context,
  fileDesc: FileDescriptorProto,
  sourceInfo: SourceInfo,
  serviceDesc: ServiceDescriptorProto
): Code {
  const { options } = ctx;
  const chunks: Code[] = [];

  chunks.push(generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc));
  chunks.push(generateServerStub(ctx, sourceInfo, serviceDesc));
  if (options.outputClientImpl) {
    chunks.push(generateClientStub(ctx, sourceInfo, serviceDesc));
    chunks.push(generateClientConstructor(fileDesc, serviceDesc));
  }

Which yes always generates the server, and only conditionally outputs the client.

I think instead of outputServices=grpc-js, what you're asking for is to be able to set outputClientImpls=grpc-js, and have that do the client stub + client constructor part of things.

Yeah, that is not supported today, but should be an easy change; if you'd like to submit a PR, that'd be great!

stephenh avatar Feb 09 '23 14:02 stephenh

right, I haven't noticed there is outputClientImpl option, but right, something like outputClientImpl=grpc-js for the client only case... so outputServices=grpc-js should not generate client code by default?

akornatskyy avatar Feb 09 '23 14:02 akornatskyy

so outputServices=grpc-js should not generate client code by default

I think in retrospect, yes, but we probably have to keep current behavior to avoid a breaking change.

So like outputServices=grpc-js would default outputClientImpl=grpc-js, but you could ideally set just outputClientImpl=gprc-js all by itself and get the behavior you want.

stephenh avatar Feb 09 '23 14:02 stephenh

I don't see the option or implementation of outputClientImpl=gprc-js in the code

gaoyang avatar Aug 21 '23 09:08 gaoyang