Skip to content

Commit

Permalink
docs: update to nexus@^1.1 api (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Jun 23, 2021
1 parent 09a8610 commit 1d31a76
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 126 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{
"gitdoc.enabled": false
"search.exclude": {
"**/dist-cjs": true,
"**/dist-esm": true,
"**/__snapshots__": true,
"yarn.lock": true
},
"gitdoc.enabled": false,
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
88 changes: 60 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ Official Prisma plugin for Nexus.
- [Project relation with custom resolver logic](#project-relation-with-custom-resolver-logic)
- [Supply custom custom scalars to your GraphQL schema](#supply-custom-custom-scalars-to-your-graphql-schema)
- [Notes](#notes)
- [Supported Versions Of Node & Prisma](#supported-versions-of-node--prisma)
- [For users of `nexus-prisma@=<0.20`](#for-users-of-nexus-prisma020)
- [For users of `prisma@=<2.17`](#for-users-of-prisma217)
- [For users of `nexus@=<1.0`](#for-users-of-nexus10)
- [Supported Versions Of Node & Prisma](#supported-versions-of-node--prisma)

<!-- tocstop -->

Expand All @@ -47,15 +50,17 @@ Official Prisma plugin for Nexus.
1. Install dependencies

```
npm add nexus-prisma graphql @prisma/client
npm add nexus-prisma nexus graphql @prisma/client
npm add --dev prisma
```

> `graphql` and `@prisma/client` are peer dependencies. `prisma` is for the Prisma CLI which you'll probably want during development.
> `nexus` `graphql` and `@prisma/client` are peer dependencies. `prisma` is for the Prisma CLI which you'll probably want during development.
> If you use `nexus@=<1.0` then you must use `t.field(<NAME>, <CONFIG>)` instead of `t.field(<CONFIG>)`. The Nexus Prisma docs assume the latter form.
1. Add a `nexus-prisma` generator block to your Prisma Schema.

> If you are using `prisma@=<2.17.x` then you must use the Nexus Prisma Prisma generator name of `nexus_prisma` instead of `nexus-prisma`. See [notes](#notes) for more detail.
> If you are using `prisma@=<2.17` then you must use the Nexus Prisma Prisma generator name of `nexus_prisma` instead of `nexus-prisma`. See [notes](#notes) for more detail.
1. Run `prisma generate` in your terminal.

Expand All @@ -71,7 +76,7 @@ generator client {
generator nexusPrisma {
provider = "nexus-prisma"
// provider = "nexus_prisma" <-- For prisma@=<2.17.x users
// provider = "nexus_prisma" <-- For prisma@=<2.17 users
}
/// This is a user!
Expand All @@ -95,7 +100,8 @@ export const schema = makeSchema({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, User.id)
t.field(User.id)
// t.field(User.id.name, User.id) <-- For nexus@=<1.0 users
}
})
]
Expand Down Expand Up @@ -167,7 +173,7 @@ objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, {
t.field({
type: User.id.type,
description: User.id.description
})
Expand Down Expand Up @@ -312,15 +318,15 @@ queryType({
objectType({
name: User.$name,
definition(t) {
t.field(User.id.name, User.id)
t.field(User.profile.name, User.profile)
t.field(User.id)
t.field(User.profile)
},
})

objectType({
name: Profile.$name,
definition(t) {
t.field(Profile.id.name, Profile.id)
t.field(Profile.id)
},
})
```
Expand Down Expand Up @@ -410,16 +416,16 @@ import { User, Profile } from 'nexus-prisma'
objectType({
name: User.$name,
definition(t) {
t.field(User.id.name, User.id)
t.field(User.profile.name, User.profile)
t.field(User.id)
t.field(User.profile)
},
})

objectType({
name: Profile.$name,
definition(t) {
t.field(Profile.id.name, Profile.id)
t.field(User.profile.name, User.profile)
t.field(Profile.id)
t.field(User.profile)
},
})
```
Expand All @@ -442,8 +448,8 @@ This limitation may be a problem for your API. There is an [issue track this tha
objectType({
name: Profile.$name,
definition(t) {
t.field(Profile.id.name, Profile.id)
t.field(User.profile.name, {
t.field(Profile.id)
t.field({
...User.profile,
type: nonNull(User.profile.type),
})
Expand Down Expand Up @@ -498,15 +504,15 @@ queryType({
objectType({
name: User.$name,
definition(t) {
t.field(User.id.name, User.id)
t.field(User.posts.name, User.posts)
t.field(User.id)
t.field(User.posts)
},
})

objectType({
name: Post.$name,
definition(t) {
t.field(Post.id.name, Post.id)
t.field(Post.id)
},
})
```
Expand Down Expand Up @@ -712,7 +718,7 @@ objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, User.id)
t.field(User.id)
}
})
```
Expand Down Expand Up @@ -746,7 +752,7 @@ objectType({
name: User.$name
description: User.$description
definition(t) {
t.field(User.id.name, User.id)
t.field(User.id)
}
})
```
Expand Down Expand Up @@ -822,8 +828,8 @@ Nexus Prisma generates default GraphQL resolvers for your model _relation fields
objectType({
name: User.$name,
definition(t) {
t.field(User.id.name, User.id)
t.field(User.posts.name, {
t.field(User.id)
t.field({
...User.posts,
async resolve(...args) {
// Your custom before-logic here
Expand All @@ -842,8 +848,8 @@ Nexus Prisma generates default GraphQL resolvers for your model _relation fields
objectType({
name: User.$name,
definition(t) {
t.field(User.id.name, User.id)
t.field(User.posts.name, {
t.field(User.id)
t.field({
...User.posts,
async resolve(...args) {
// Your custom logic here
Expand Down Expand Up @@ -877,11 +883,37 @@ makeSchema({

## Notes

- Versions of `nexus-prisma` package prior to `0.20` were a completely different version of the API, and had also become deprecated at one point to migrate to `nexus-plugin-prisma` when Nexus Framework was being worked on. All of that is history.
### For users of `nexus-prisma@=<0.20`

Versions of `nexus-prisma` package prior to `0.20` were a completely different version of the API, and had also become deprecated at one point to migrate to `nexus-plugin-prisma` when Nexus Framework was being worked on. All of that is history.

### For users of `prisma@=<2.17`

If you are using `prisma@=<2.17` then you must use the Nexus Prisma Prisma generator name of `nexus_prisma` instead of `nexus-prisma`. This is because prior to `prisma@2.18` there was a hardcode check for `nexus-prisma` that would fail with an error message about a now-old migration.

### For users of `nexus@=<1.0`

- If you are using `prisma@=<2.17.x` then you must use the Nexus Prisma Prisma generator name of `nexus_prisma` instead of `nexus-prisma`. This is because prior to `prisma@2.18.x` there was a hardcode check for `nexus-prisma` that would fail with an error message about a now-old migration.
The [release of Nexus 1.1](https://github.com/graphql-nexus/nexus/releases/tag/1.0.0) introduced an overload to `t.field` allowing improved usage of Nexus Prisma. The difference is as follows. Note if you prefer the older way that is and always will be supported too.

```diff 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) // <-- for nexus@>=1.1 users
- t.field(User.id.name, User.id) // <-- For nexus@=<1.0 users
}
})
]
})
```

## Supported Versions Of Node & Prisma
### Supported Versions Of Node & Prisma

We only officially support what we test.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"jest-watch-typeahead": "0.6.4",
"lodash": "^4.17.21",
"markdown-toc": "^1.2.0",
"nexus": "^1.0.0",
"nexus": "^1.1.0",
"nodemon": "^2.0.7",
"prettier": "2.3.1",
"prisma": "2.25.0",
Expand Down
4 changes: 2 additions & 2 deletions src/generator/helpers/JSDocTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function modelExample(model: DMMF.Model): string {
* name: ${model.name}.$name
* description: ${model.name}.$description
* definition(t) {
* t.field(${model.name}.id.name, ${model.name}.id)
* t.field(${model.name}.id)
* }
* })
`
Expand Down Expand Up @@ -164,7 +164,7 @@ function fieldExample({ model, field }: FieldModelParams): string {
* name: ${model.name}.$name
* description: ${model.name}.$description
* definition(t) {
* t.field(${model.name}.${field.name}.name, ${model.name}.${field.name})
* t.field(${model.name}.${field.name})
* }
* })
`
Expand Down
2 changes: 1 addition & 1 deletion src/generator/models/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function prismaFieldToNexusResolver(
*
* So:
*
* t.field(M1.Foo.bar.$name, M1.Foo.bar)
* t.field(M1.Foo.bar)
*
* where `bar` is a scalar prisma field would have NO resolve generated and thus default Nexus as mentioned would
* think that `bar` field WILL be present on the source type. This is, again, mostly moot since most Nexus Prisma
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ exports[`When bundled custom scalars are used the project type checks and genera
* Do not make changes to this file directly
*/
import * as PrismaClient from \\".prisma/client\\"
import { core } from \\"nexus\\"
import type * as PrismaClient from \\".prisma/client\\"
import type { core } from \\"nexus\\"
declare global {
interface NexusGenCustomInputMethods<TypeName extends string> {
/**
Expand Down Expand Up @@ -243,6 +243,8 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {
}
interface NexusGenPluginInputTypeConfig<TypeName extends string> {
}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
Expand Down
14 changes: 8 additions & 6 deletions tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ it('When bundled custom scalars are used the project type checks and generates e
enumType(SomeEnumA),
queryType({
definition(t) {
t.list.field('bars', {
t.list.field({
name: 'bars',
type: 'Bar',
resolve(_, __, ctx) {
return ctx.prisma.bar.findMany()
Expand All @@ -197,21 +198,22 @@ it('When bundled custom scalars are used the project type checks and generates e
objectType({
name: Bar.$name,
definition(t) {
t.field(Bar.foo.name, Bar.foo)
t.field(Bar.foo)
},
}),
objectType({
name: Foo.$name,
definition(t) {
t.field('someEnumA', {
t.field({
name: 'someEnumA',
type: 'SomeEnumA',
})
t.json('JsonManually')
t.dateTime('DateTimeManually')
t.bytes('BytesManually')
t.field(Foo.someJsonField.name, Foo.someJsonField)
t.field(Foo.someDateTimeField.name, Foo.someDateTimeField)
t.field(Foo.someBytesField.name, Foo.someBytesField)
t.field(Foo.someJsonField)
t.field(Foo.someDateTimeField)
t.field(Foo.someBytesField)
},
}),
]
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ testIntegration({
objectType({
name: Foo.$name,
definition(t) {
t.field(Foo.json.$name, Foo.json)
t.field(Foo.json)
},
}),
queryType({
definition(t) {
t.field('foos', {
t.field({
name: 'foos',
type: list(Foo.name),
resolve(_, __, ctx) {
return ctx.prisma.foo.findMany()
Expand Down
Loading

0 comments on commit 1d31a76

Please sign in to comment.