proto-converter2
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

proto-converter2

This is a custom version, based on the "proto-converter"

Converts schema definitions in Protocol Buffer (proto3) to GraphQL(@emzeq/proto2graphql inspires me).
you can also convert to TypeScript or anything you want by plugins.

Installation

Install with npm:

npm install --save-dev proto-converter

Usage

In your proto-converter.config.js at the root directory of project:

const { buildGql, buildInterface, buildGraphql } = require('proto-converter')
const {
  buildService,
  buildModule,
  buildResolver,
} = require('proto-converter/lib/plugins/nestjs/index')

module.exports = {
  // the directory contains proto files
  sourcePath: './proto',
  // the output directory
  outputPath: 'src/test-result',
  rootDir: 'src',
  // An array of proto-converter plugins
  plugins: [
    buildGraphql,
    buildGql,
    buildInterface,
    buildResolver,
    buildService,
    buildModule,
  ],
}

In package.json:

{
  "scripts": {
    "convert": "proto-converter"
  }
}

then run with npm:

npm run convert

after that, you will anwser two questions:

# the proto path base on "sourcePath" of config, or absolute path
# for this one, it would be: /your-project-path/proto.develop/helloword/hi.proto
protoPath: helloword/hi.proto
# Optional. the "serviceName" would be the prefix of schema
# and the new fold name of new files to be location
serviceName: converter

for example:

syntax = "proto3";

service BuildRequest {
  // get proto config
  rpc GetConfig (GetConfigRequest) returns (GetConfigResponse) {
  }
}

message GetConfigRequest {
  string config_name = 1;
}

message GetConfigResponse {
  string name = 1;
  string path = 2;
}

the result of graphql schema:

type Query {
  """
  get proto config
  """
  buildRequest_getConfig(req: GetConfigRequest): GetConfigResponse
}

type Converter_GetConfigRequest {
  config_name: String
}

type Converter_GetConfigResponse {
  name: String
  path: String
}

Plugins

A plugin is a function which:

type ConverterPlugin = (protoInfo: ProtoInfo) => void

about the ProtoInfo:

interface ProtoInfo {
  root: protobuf.Root
  // the main proto object of current processing proto file
  proto: protobuf.Namespace
  // services in main proto
  services: protobuf.Service[] | null
  // all messages be used including nested\import messages
  messages: EnhancedReflectionObject[]
  config: Required<ConverterConfig>
}

type ConverterPlugin = (protoInfo: ProtoInfo) => void

interface EnhancedReflectionObject extends protobuf.ReflectionObject {
  // if the "type" is a request type
  isInput?: boolean
}

interface ConverterConfig {
  // absolute path of current processing proto file
  protoPath: string
  plugins: ConverterPlugin[]
  serviceName?: string
  sourcePath?: string
  outputPath?: string
  rootDir?: string
}

about the Protobuf, see protobuf.js

Configuration

The configuration file are optional, but it is convenient and thus recommended.

It is called proto-converter.config.js and sits in the root directory of your project.

module.exports = {
  // optional. The directory contains proto files
  // defaults to the root directory of your project.
  sourcePath: './proto',
  // optional. The directory in which all generated files are placed.
  // defaults to the root directory of your project.
  outputPath: 'src/graphql',
  // optional. used by nestjs-plugins currently.
  // defaults to 'src'.
  rootDir: 'src',
  // required. An array of proto-converter plugins
  plugins: [],
}

Convention for proto

  1. tag the required params if it requires for this request
message GetConfigResponse {
   // required, valid tag
  string field_a = 1;
  //       required      valid tag
  string field_b = 2;
  string field_c = 3; // required valid tag
  // something required  invalid tag
  string field_d = 4;
  // something
  string field_e = 5; // required invalid tag this comment will be ignore
}
  1. tag the map type, just for golang
message GetConfigResponse {
  // [id,name] valid tag,return { id: string, name: string }
  map<string,string> field_a = 1;
  // [ id , name ] valid tag
  map<string,string> field_b = 2;
  // no tag, return Record<string|number, any> as the
  // field value of types
  map<string,string> field_d = 2;
}

Dependencies (9)

Dev Dependencies (16)

Package Sidebar

Install

npm i proto-converter2

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

172 kB

Total Files

94

Last publish

Collaborators

  • chenxf