npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

proto-converter

v0.4.7

Published

converts proto to GraphQL schema

Downloads

9

Readme

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;
}