Skip to content

Commit

Permalink
feat: add prisma sans plugin example (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Kuhrt authored Jul 28, 2020
1 parent 6b4b600 commit f067b7b
Show file tree
Hide file tree
Showing 10 changed files with 3,154 additions and 5 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# examples
# Nexus Examples

This repo includes various examples using [Nexus](https://nexusjs.org).
This repo includes various examples using [Nexus](https://nexusjs.org). Each directory here contains one runnable example. The naming pattern of directories is designed to help navigate them. The gist is as follows (if you are curious about the exact rules see the [contributing guide](/CONTRIBUTING.md)):

- `plugin-*` Are for demonstrating a plugin
- `plugins-*` Are for demonstrating plugins used together
- `with-*` Are for demonstrating an integration with some other tool Notably _without_ using the plugin system.

Below you will find the examples indexed by various dimensions. So if you're curious about a topic like "authentication" or "databases" you can easily find all examples that relate to that.

If you are an expert in some tool that Nexus could integrate with but there is no example here yet for it, we would welcome a contribution for it! To get started please refer to the [contributing guide](/CONTRIBUTING.md). 🙏🏻

### Basic

- [hello-world](/hello-world)

### Plugins
### By Plugin

#### Prisma

Expand All @@ -24,7 +32,12 @@ This repo includes various examples using [Nexus](https://nexusjs.org).

- [plugins-prisma-and-jwt-auth-and-shield](/plugins-prisma-and-jwt-auth-and-shield)

### Integrations
### By Integration

#### Prisma

- [with-prisma](/with-prisma)
- [plugin-prisma](/plugin-prisma)

#### NextJS

Expand All @@ -35,7 +48,7 @@ This repo includes various examples using [Nexus](https://nexusjs.org).

- [with-nextjs-and-vercel-and-plugins-prisma](/with-nextjs-and-vercel-and-plugins-prisma)

### Domains
### By Domain

#### Deployment

Expand All @@ -45,3 +58,8 @@ This repo includes various examples using [Nexus](https://nexusjs.org).

- [plugins-prisma-and-jwt-auth-and-shield](/plugins-prisma-and-jwt-auth-and-shield)
- [plugins-prisma-and-jwt-auth](/plugins-prisma-and-jwt-auth)

#### Databases

- [with-prisma](/with-prisma)
- [plugin-prisma](/plugin-prisma)
9 changes: 9 additions & 0 deletions with-prisma/.gitignore
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
17 changes: 17 additions & 0 deletions with-prisma/.vscode/launch.json
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"
}
]
}
13 changes: 13 additions & 0 deletions with-prisma/README.md
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
```
24 changes: 24 additions & 0 deletions with-prisma/api.graphql
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
}
53 changes: 53 additions & 0 deletions with-prisma/api.ts
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()
},
})
},
})
Loading

0 comments on commit f067b7b

Please sign in to comment.