Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update installation and usage instructions #73

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ Protobuf-First GraphQL Schemas with [GraphQL Nexus](https://nexusjs.org/)

## Installation

```
yarn add nexus graphql
```sh
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
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
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:

```
```sh
protoc \
-I ./node_modules/protoc-gen-nexus/include \
-I <YOUR_PROTO_PATH> \
Expand All @@ -40,8 +43,57 @@ protoc \
path/to/file.proto
```

If you generate code with protobufjs, add `use_protobufjs` to `nexus_opt`
#### with protobufjs

proto-nexus supports protobufjs with static code generated by [protobufjs CLI](https://github.com/protobufjs/protobuf.js#command-line).

```sh
# `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.

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


### Make schema
See also [Best Practices - GraphQL Nexus](https://nexusjs.org/docs/guides/best-practices#consistent-file-structure-for-graphql-type-imports).

```typescript
// 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`.

```typescript
import { makeSchema } from "nexus";
import * as allTypes from "./schema/types";

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