forked from graphql-nexus/nexus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
64 lines (59 loc) · 1.55 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { PrismaClient } from '@prisma/client'
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { makeSchema, nullable, objectType, queryType, stringArg } from 'nexus'
import * as path from 'path'
const prisma = new PrismaClient()
const apollo = new ApolloServer({
context: () => ({ prisma }),
schema: makeSchema({
sourceTypes: {
modules: [{ module: '.prisma/client', alias: 'PrismaClient' }],
},
contextType: {
module: path.join(__dirname, 'context.ts'),
export: 'Context',
},
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', {
resolve(parent) {
return parent.name
},
})
},
}),
queryType({
definition(t) {
t.list.field('users', {
type: 'User',
args: {
world: nullable(stringArg()),
},
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`)
})