mirror of
https://github.com/kou029w/_.git
synced 2025-01-30 22:08:02 +00:00
create nexus-with-prisma
This commit is contained in:
parent
f44b75c613
commit
fbc5a580bc
8 changed files with 2124 additions and 0 deletions
10
nexus-with-prisma/.gitignore
vendored
Normal file
10
nexus-with-prisma/.gitignore
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# Node
|
||||||
|
node_modules
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
migrations
|
||||||
|
.nexus
|
||||||
|
db.sqlite
|
||||||
|
build
|
24
nexus-with-prisma/README.md
Normal file
24
nexus-with-prisma/README.md
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# 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
|
||||||
|
```
|
||||||
|
|
||||||
|
Terminal 1
|
||||||
|
|
||||||
|
```
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Terminal 2
|
||||||
|
|
||||||
|
```
|
||||||
|
npm run dev:typecheck
|
||||||
|
```
|
12
nexus-with-prisma/api.graphql
Normal file
12
nexus-with-prisma/api.graphql
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
### This file was generated by Nexus Schema
|
||||||
|
### Do not make changes to this file directly
|
||||||
|
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
users(world: String): [User!]!
|
||||||
|
}
|
||||||
|
|
||||||
|
type User {
|
||||||
|
id: ID!
|
||||||
|
name: String
|
||||||
|
}
|
62
nexus-with-prisma/api.ts
Normal file
62
nexus-with-prisma/api.ts
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
import { makeSchema, objectType, queryType, stringArg } from '@nexus/schema'
|
||||||
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
import { ApolloServer } from 'apollo-server-express'
|
||||||
|
import express from 'express'
|
||||||
|
import * as path from 'path'
|
||||||
|
|
||||||
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
|
const apollo = new ApolloServer({
|
||||||
|
context: () => ({ prisma }),
|
||||||
|
schema: makeSchema({
|
||||||
|
typegenAutoConfig: {
|
||||||
|
contextType: '{ prisma: PrismaClient.PrismaClient }',
|
||||||
|
sources: [{ source: '.prisma/client', alias: 'PrismaClient' }],
|
||||||
|
},
|
||||||
|
outputs: {
|
||||||
|
typegen: path.join(
|
||||||
|
__dirname,
|
||||||
|
'node_modules/@types/nexus-typegen/index.d.ts',
|
||||||
|
),
|
||||||
|
schema: path.join(__dirname, './api.graphql'),
|
||||||
|
},
|
||||||
|
shouldExitAfterGenerateArtifacts: Boolean(
|
||||||
|
process.env.NEXUS_SHOULD_EXIT_AFTER_REFLECTION,
|
||||||
|
),
|
||||||
|
types: [
|
||||||
|
objectType({
|
||||||
|
name: 'User',
|
||||||
|
definition(t) {
|
||||||
|
t.id('id')
|
||||||
|
t.string('name', {
|
||||||
|
nullable: true,
|
||||||
|
resolve(parent) {
|
||||||
|
return parent.name
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
queryType({
|
||||||
|
definition(t) {
|
||||||
|
t.list.field('users', {
|
||||||
|
type: 'User',
|
||||||
|
args: {
|
||||||
|
world: stringArg({ required: false }),
|
||||||
|
},
|
||||||
|
resolve(_root, _args, ctx) {
|
||||||
|
return ctx.prisma.user.findMany()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const app = express()
|
||||||
|
|
||||||
|
apollo.applyMiddleware({ app })
|
||||||
|
|
||||||
|
app.listen(4000, () => {
|
||||||
|
console.log(`🚀 GraphQL service ready at http://localhost:4000/graphql`)
|
||||||
|
})
|
32
nexus-with-prisma/package.json
Normal file
32
nexus-with-prisma/package.json
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
{
|
||||||
|
"name": "with-prisma",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"license": "UNLICENSED",
|
||||||
|
"scripts": {
|
||||||
|
"build": "yarn build:reflection && tsc",
|
||||||
|
"build:reflection": "NEXUS_SHOULD_EXIT_AFTER_REFLECTION=true ts-node api",
|
||||||
|
"dev": "ts-node-dev --transpile-only api",
|
||||||
|
"dev:migrate": "prisma migrate save --experimental -c && prisma migrate up --experimental -c",
|
||||||
|
"dev:typecheck": "tsc --watch --noEmit",
|
||||||
|
"format": "npx prettier --write './**/*.{ts,md}'",
|
||||||
|
"start": "NODE_ENV=production node .nexus/build"
|
||||||
|
},
|
||||||
|
"prettier": {
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "all"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@nexus/schema": "^0.15.0",
|
||||||
|
"@prisma/client": "2.3.0",
|
||||||
|
"apollo-server-express": "^2.17.0",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"graphql": "^15.3.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@prisma/cli": "^2.3.0",
|
||||||
|
"prettier": "2.0.5",
|
||||||
|
"ts-node-dev": "^1.0.0-pre.62",
|
||||||
|
"typescript": "^4.0.2"
|
||||||
|
}
|
||||||
|
}
|
45
nexus-with-prisma/prisma/schema.prisma
Normal file
45
nexus-with-prisma/prisma/schema.prisma
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
datasource db {
|
||||||
|
provider = "sqlite"
|
||||||
|
url = "file:db.sqlite"
|
||||||
|
}
|
||||||
|
|
||||||
|
generator prisma_client_js {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
}
|
||||||
|
|
||||||
|
model Blog {
|
||||||
|
id Int @default(autoincrement()) @id
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
name String
|
||||||
|
viewCount Int @default(0)
|
||||||
|
posts Post[]
|
||||||
|
authors User[]
|
||||||
|
}
|
||||||
|
|
||||||
|
model User {
|
||||||
|
id Int @default(autoincrement()) @id
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
name String?
|
||||||
|
posts Post[]
|
||||||
|
blogId Int?
|
||||||
|
blog Blog? @relation(fields: [blogId], references: [id])
|
||||||
|
rating Float
|
||||||
|
}
|
||||||
|
|
||||||
|
model Post {
|
||||||
|
id Int @default(autoincrement()) @id
|
||||||
|
title String
|
||||||
|
tags Tag[] @relation(references: [id])
|
||||||
|
blogId Int?
|
||||||
|
blog Blog? @relation(fields: [blogId], references: [id])
|
||||||
|
User User? @relation(fields: [userId], references: [id])
|
||||||
|
userId Int?
|
||||||
|
}
|
||||||
|
|
||||||
|
model Tag {
|
||||||
|
id Int @default(autoincrement()) @id
|
||||||
|
label String
|
||||||
|
posts Post[] @relation(references: [id])
|
||||||
|
}
|
11
nexus-with-prisma/tsconfig.json
Normal file
11
nexus-with-prisma/tsconfig.json
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"target": "es2018",
|
||||||
|
"module": "commonjs",
|
||||||
|
"strict": true,
|
||||||
|
"outDir": "build",
|
||||||
|
"rootDir": "."
|
||||||
|
}
|
||||||
|
}
|
1928
nexus-with-prisma/yarn.lock
Normal file
1928
nexus-with-prisma/yarn.lock
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue