-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
106 lines (100 loc) · 2.28 KB
/
schema.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Context } from './context'
import {
intArg,
makeSchema,
objectType,
} from 'nexus'
const Post = objectType({
name: 'Post',
definition(t) {
t.nonNull.int('id')
t.nonNull.string('title')
t.string('content')
t.field('author', {
type: 'User',
resolve: (parent, _, context: Context) => {
return context.prisma.post
.findUnique({
where: { id: parent.id || undefined },
})
.author()
},
})
},
})
const User = objectType({
name: 'User',
definition(t) {
t.nonNull.int('id')
t.string('name')
t.nonNull.string('email')
t.nonNull.list.nonNull.field('posts', {
type: 'Post',
resolve: (parent, _, context: Context) => {
return context.prisma.user
.findUnique({
where: { id: parent.id || undefined },
})
.posts()
},
})
},
})
const Query = objectType({
name: 'Query',
definition(t) {
t.nonNull.list.nonNull.field('offsetPagination', {
type: 'Post',
args: {
skip: intArg(),
take: intArg(),
},
resolve: (_parent, args, context: Context) => {
return context.prisma.post.findMany({
skip: args.skip || undefined,
take: args.take || undefined,
})
},
})
t.nonNull.list.nonNull.field('cursorPagination', {
type: 'Post',
args: {
skip: intArg(),
take: intArg(),
cursor: intArg(),
},
resolve: (_parent, args, context: Context) => {
return context.prisma.post.findMany({
skip: args.skip || undefined,
take: args.take || undefined,
cursor: {
id: args.cursor || undefined,
},
})
},
})
},
})
export const schema = makeSchema({
types: [
Query,
Post,
User
],
outputs: {
schema: __dirname + '/../schema.graphql',
typegen: __dirname + '/generated/nexus.ts',
},
contextType: {
module: require.resolve('./context'),
export: 'Context',
},
sourceTypes: {
modules: [
{
module: '@prisma/client',
alias: 'prisma',
},
],
},
})