-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prisma sans plugin example (#66)
- Loading branch information
Jason Kuhrt
authored
Jul 28, 2020
1 parent
6b4b600
commit f067b7b
Showing
10 changed files
with
3,154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Node | ||
node_modules | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
migrations | ||
.nexus | ||
db.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Note: You can delete this file if you're not using vscode | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug Nexus App", | ||
"protocol": "inspector", | ||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/nexus", | ||
"runtimeArgs": ["dev"], | ||
"args": ["api/app.ts"], | ||
"sourceMaps": true, | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Nexus Example With Prisma | ||
|
||
This example shows how to use Nexus with [Prisma](https://prisma.io) without the [Prisma plugin for Nexus](https://nxs.li/plugins/prisma). This approach is lower-level and here for reference reasons. Generally, you would want to use the Prisma plugin. | ||
|
||
### Try It | ||
|
||
``` | ||
npm install | ||
npx prisma generate | ||
npx prisma migrate save --experimental | ||
npx prisma migrate up --experimental | ||
npx nexus dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
### This file was generated by Nexus Schema | ||
### Do not make changes to this file directly | ||
|
||
|
||
""" | ||
A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the | ||
`date-time` format outlined in section 5.6 of the RFC 3339 profile of the ISO | ||
8601 standard for representation of dates and times using the Gregorian calendar. | ||
""" | ||
scalar DateTime | ||
|
||
""" | ||
The `JSON` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). | ||
""" | ||
scalar Json | ||
|
||
type Query { | ||
users(world: String): [User!] | ||
} | ||
|
||
type User { | ||
id: ID | ||
name: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as Prisma from '@prisma/client' | ||
import { on, schema } from 'nexus' | ||
|
||
/** | ||
* Without the Prisma plugin you need to manually expose Prisma types into your backing types. | ||
*/ | ||
|
||
// todo https://github.com/graphql-nexus/nexus/issues/473#issuecomment-665171477 | ||
export type User = Prisma.User | ||
|
||
on.start(() => { | ||
/** | ||
* Without the Prisma plugin you need to manaully contruct your Prisma client instance. | ||
* With the plugin you can also still create your Prisma client instance if you need to. | ||
*/ | ||
const db = new Prisma.PrismaClient() | ||
|
||
/** | ||
* Without the Prisma plugin you need to manually expose your Prisma client on the context. | ||
*/ | ||
schema.addToContext(() => { | ||
return { db } | ||
}) | ||
}) | ||
|
||
schema.objectType({ | ||
name: 'User', | ||
/** | ||
* Without the Prisma plugin you need to manually map your Prisma types to the backing | ||
* types of your GrpahQL objects. With Prisma plugin when the type names between Prisma | ||
* and GraphQL layers are the same, they are automatically mapped. But you can still | ||
* control this manually with the Prisma plugin. | ||
*/ | ||
rootTyping: 'User', | ||
definition(t) { | ||
t.id('id') | ||
t.string('name') | ||
}, | ||
}) | ||
|
||
schema.queryType({ | ||
definition(t) { | ||
t.list.field('users', { | ||
type: 'User', | ||
args: { | ||
world: schema.stringArg({ required: false }), | ||
}, | ||
resolve(_root, _args, ctx) { | ||
return ctx.db.user.findMany() | ||
}, | ||
}) | ||
}, | ||
}) |
Oops, something went wrong.