protoc-gen-nexus
v0.8.1
Published
Generate DSL for GraphQL Nexus from Protocol Buffers IDL
Downloads
111
Maintainers
Readme
ProtoNexus
Protobuf-First GraphQL Schemas with GraphQL Nexus
Packages
| package | description | version |
| ------- | ----------- | ------- |
| protoc-gen-nexus | protoc
plugin for generating Nexus type definitions | |
| @proto-nexus/google-protobuf | Runtime library | |
| @proto-nexus/protobufjs | Runtime library | |
| @proto-nexus/proto-fields-plugin | Nexus plugin for building subset types from proto-nexus's artifacts | |
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,
},
},
})