protoc-gen-nexus
TypeScript icon, indicating that this package has built-in type declarations

0.7.0 • Public • Published

ProtoNexus

Protobuf-First GraphQL Schemas with GraphQL Nexus

Packages

package description version
protoc-gen-nexus protoc plugin for generating Nexus type definitions npm version
@proto-nexus/google-protobuf Runtime library npm version
@proto-nexus/protobufjs Runtime library npm version
@proto-nexus/proto-fields-plugin Nexus plugin for building subset types from proto-nexus's artifacts npm version

Installation

yarn add nexus graphql  # required as a peer dependency
yarn add --dev protoc-gen-nexus

# if you generate code with `protoc --js_out`
yarn add @proto-nexus/google-protobuf \
  google-protobuf @types/google-protobuf  # required as a peer dependency

# if you generate code with protobufjs
yarn add @proto-nexus/protobufjs \
  protobufjs  # required as a peer dependency

Usage

Generate GraphQL Types from Protobuf IDL

To generate Nexus type definitions from .proto files, you need to invoke following command:

protoc \
  -I ./node_modules/protoc-gen-nexus/include \
  -I <YOUR_PROTO_PATH> \
  --plugin=protoc-gen-nexus=`yarn bin protoc-gen-nexus` \
  --nexus_out=<DIST_DIR> \
  --nexus_opt="import_prefix=<YOUR_PROTO_PATH_OR_PACKAGE>" \
  path/to/file.proto

with protobufjs

proto-nexus supports protobufjs with static code generated by protobufjs CLI.

# `target`, `wrap` and `force-message` options are required.
# An output file name must be `index.js`.
pbjs \
  --target static-module \
  --wrap commonjs \
  --force-message \
  --path <YOUR_PROTO_PATH> \
  --out "<DIST_DIR>/index.js" \
  path/to/package/*.proto

# An output file name must be `index.d.ts`
pbts --out "<DIST_DIR>/index.d.ts" "<DIST_DIR>/index.js"

And protoc-gen-nexus requires use_protobufjs option.

  --nexus_opt="import_prefix=<YOUR_PROTO_PATH_OR_PACKAGE>,use_protobufjs" \

Make schema

See also Best Practices - GraphQL Nexus.

// src/schema/types/index.ts

// Export generated types
export * from "./path/to/generated_types/...";
export * from "./path/to/generated_types/...";
// ...

When makeSchema, abstractTypeStrategies must have isTypeOf: true.

import { makeSchema } from "nexus";
import "@proto-nexus/google-protobuf";  //  should import runtime library
import * as allTypes from "./schema/types";

export const schema = makeSchema({
  types: alltypes,
  output: { /* ... */ },
  features: {
    abstractTypeStrategies: {
      isTypeOf: true,
    },
  },
})

Author

Package Sidebar

Install

npm i protoc-gen-nexus

Weekly Downloads

90

Version

0.7.0

License

MIT

Unpacked Size

748 kB

Total Files

95

Last publish

Collaborators

  • izumin5210