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

feat: emit model scalar fields #5

Merged
merged 13 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.yalc
.vscode
.github
node_modules
dist
dist-esm
tests
jest.config.ts
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["tsconfig.json", "tests/tsconfig.json"]
},
"plugins": ["@typescript-eslint", "only-warn"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"overrides": [],
"rules": {
// TypeScript makes these safe & effective
"no-case-declarations": "off",
// Same approach used by TypeScript noUnusedLocals
"@typescript-eslint/no-unused-vars": ["warn", { "varsIgnorePattern": "^_", "argsIgnorePattern": "^_" }]
}
}
22 changes: 22 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ on:
- pull_request

jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14.x
- name: Install Dependencies
run: yarn --frozen-lockfile
- run: yarn -s format:check

eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14.x
- name: Install Dependencies
run: yarn --frozen-lockfile
- run: yarn -s lint:check

test:
strategy:
matrix:
Expand Down
4 changes: 4 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
...require('@prisma-labs/prettier-config'),
jsdocParser: true,
}
47 changes: 47 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "dev:link",
"dependsOn": ["dev:yalc", "dev:ts"]
},
{
"label": "tdd",
"type": "shell",
"command": "yarn -s tdd",
"presentation": {
"group": "test",
"focus": true,
"panel": "shared",
"clear": true
}
},

// Lower level task building blocks

{
"label": "dev:yalc",
"type": "shell",
"command": "yarn -s dev:yalc",
"presentation": {
"group": "dev",
"focus": false,
"panel": "shared",
"clear": true
}
},
{
"label": "dev:ts",
"type": "shell",
"command": "yarn -s dev:ts",
"presentation": {
"group": "dev",
"focus": false,
"panel": "shared",
"clear": true
}
}
]
}
46 changes: 46 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
## Tests

- We disable `kleur` colors so snapshots do not have them. It would be nice to put the DX of colors into tests but needs some work. Node 12/14 results in different codes, [thus different snapshots](https://github.com/prisma/nexus-prisma/pull/3#issuecomment-782432471). See test-mode feature request here: https://github.com/lukeed/kleur/issues/47#issue-812419257.

## Link-Like Development

Sometimes it is useful to use a [link workflow](https://docs.npmjs.com/cli/v6/commands/npm-link). This means working on a local checkout of the Nexus Prisma source code, while trying it out in a project as local on your machine. This can be great for feeling out ideas.

Linking with Nexus Prisma is problematic because some modules in the Nexus Prisma source do file path lookups relative to where they are on disk. These lookups expect to be in the project that is using Nexus Prisma. Regular link workflows violate this assumptions.

The solution is to use [Yalc](https://github.com/wclr/yalc).

#### Instructions

Definitions:

- `Nexus Prisma`: Your local checkout of the source code.
- `Project`: Some project that you are trying out your local version of Nexus Prisma on.

One-time:

1. Install yalc on your machine `npm -g add yalc`.
1. Install nodemon on your machine `npm -g add nodemon`.

Usually-one-time:

1. In `Project` run `yalc add nexus-prisma`.

Every-time:

1. In `Nexus Prisma` run the VSCode task `dev:link`.
1. In `Project` run `nodemon --watch '.yalc/**/*' --exec 'yarn -s prisma generate'`

With all this in place, the chain reaction goes like this:

1. You change `Nexus Prisma`
1. `Nexus Prisma` TS in watch mode emits into `dist`
1. `Nexus Prisma` `nodemon` reacts to this, runs `yalc push`, Yalc emits into `Project`'s `.yalc` dir
1. `Project` `nodemon` reacts to this, runs `prisma generate`
1. You try things out with newly generated Nexus Prisma in `Project`!

One issues are being worked out related to `bin` and `chmod`: https://github.com/wclr/yalc/issues/156

If you change a dependency in `Nexus Prisma` while working (especially adding a new one) you will need to remove the `node_modules` in `Project` and re-install e.g. `yarn install`.

## Debugging

- We use `debug`. Enable by setting envar `DEBUG=nexus-prisma*`
- If you set envar `NP_DEBUG=true` then Nexus Prisma will write `dmmf.json` to CWD at generation time.
170 changes: 169 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,177 @@

Official Prisma plugin for Nexus

## Example

```prisma

generator client {
provider = "prisma-client-js"
}

generator nexusPrisma {
// This is a temporary name, soon will be just "nexus-prisma" (pending a change in Prisma core).
provider = "nexus-prisma-2"
}


/// This is a user!
model User {
/// This is an id!
id String @id
}
```

```
$ prisma generate
```

```ts
import { User } from 'nexus-prisma'
import { makeSchema, objectType } from 'nexus'

export const schema = makeSchema({
types: [
objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, User.id)
}
})
]
})
```

## Features

### Opt-outable friendly runtime peer dependency checks
> **Note**: ⛑ The following use abbreviated examples that skip a complete setup of passing Nexus type definition to Nexus `makeSchema`. If you are new to Nexus, Consider reading the [official Nexus tutorial](https://nxs.li/tutorial) before jumping into Nexus Prisma.

### Type-safe seamless generated library code

Following the same philosophy as Prisma Client, Nexus Prisma uses generation to create an API that feels tailor made for your project.

```prisma
model User {
id String @id
}
```

```ts
import { User } from 'nexus-prisma'
import { objectType } from 'nexus'

objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, {
type: User.id.type,
description: User.id.description
})
}
})
```

### Prisma ID field to GraphQL ID scalar type mapping

All `@id` fields in your Prisma Schema get projected as `ID` types, not `String` types.

```prisma
model User {
id String @id
}
```

```ts
import { User } from 'nexus-prisma'
import { objectType } from 'nexus'

objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, User.id)
}
})
```

```graphql
type User {
id: ID
}
```

### Prisma Schema model & field documentation re-use

#### GraphQL documentation for your API clients

```prisma
/// A user.
model User {
/// A stable identifier to find users by.
id String @id
}
```

```ts
import { User } from 'nexus-prisma'
import { objectType } from 'nexus'

User.$description // JSDoc: A user.
User.id.description // JSDoc: A stable identifier to find users by.

objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, User.id)
}
})
```

```graphql
"""
A user.
"""
type User {
"""
A stable identifier to find users by.
"""
id: ID
}
```

#### Internal JSDoc for your team

```prisma
/// A user.
model User {
/// A stable identifier to find users by.
id String @id
}
```

```ts
import { User } from 'nexus-prisma'

User // JSDoc: A user.
User.id // JSDoc: A stable identifier to find users by.
```

### Refined DX

These are finer points that aren't perhaps worth a top-level point but none the less add up toward a thoughtful developer experience.

##### Default JSDoc Prompts

Fields and models that you do not document will result in a helpful default JSDoc that teaches you about this.

##### Runtime Proxy

When your project is in a state where the generated Nexus Prisma part is missing (new repo clone, reinstalled deps, etc.) Nexus Prisma gives you a default runtime export named `PleaseRunPrismaGenerate` and will error with a clear message.

##### Opt-outable friendly runtime peer dependency checks

When `nexus-prisma` is imported it will validate that your project has peer dependencies setup correctly.

Expand Down
Loading