-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
203 lines (182 loc) · 4.75 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { nexusPrismaPlugin } from 'nexus-prisma'
import { idArg, makeSchema, objectType, stringArg } from 'nexus'
const Dog = objectType({
name: 'Dog',
definition(t) {
t.model.id()
t.model.name()
t.model.attributes({ type: 'Attribute', })
t.field('isGoodDog', { type: 'Boolean', resolve: () => true })
t.model.score()
// t.model.email()
// t.model.posts({
// pagination: false,
// })
},
})
const Attribute = objectType({
name: 'Attribute',
definition(t) {
t.model.id()
t.model.attributeTemplate()
t.model.value()
// t.model.createdAt()
// t.model.updatedAt()
// t.model.title()
// t.model.content()
// t.model.published()
// t.model.author()
},
})
const AttributeTemplate = objectType({
name: 'AttributeTemplate',
definition(t) {
t.model.id()
t.model.name()
t.model.choices()
t.model.description()
}
})
const Query = objectType({
name: 'Query',
definition(t) {
t.crud.dog()
t.crud.dogs({ pagination: false })
t.crud.attributeTemplates()
// t.crud.post({
// alias: 'post',
// })
// t.list.field('feed', {
// type: 'Post',
// resolve: (_parent, _args, ctx) => {
// return ctx.photon.posts.findMany({
// where: { published: true },
// })
// },
// })
// t.list.field('filterPosts', {
// type: 'Post',
// args: {
// searchString: stringArg({ nullable: true }),
// },
// resolve: (_, { searchString }, ctx) => {
// return ctx.photon.posts.findMany({
// where: {
// OR: [
// { title: { contains: searchString } },
// { content: { contains: searchString } },
// ],
// },
// })
// },
// })
},
})
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.crud.createOneDog()
t.crud.deleteOneDog()
t.field('dogAddAttribute', {
type: 'Dog',
args: {
dogName: stringArg({ nullable: false }),
templateName: stringArg({ nullable: false }),
value: stringArg({ nullable: false })
},
resolve: async (_, { dogName, templateName, value }, ctx) => {
const template = await ctx.photon.attributeTemplates.findOne({
where: {name: templateName}
})
if (!template) {
throw new Error('template not found')
}
const dog = await ctx.photon.dogs.findOne({
include: { attributes: true },
where: { name: dogName }
});
if (!dog) {
throw new Error('dog not found')
}
if (!template.choices.includes(value)) {
throw new Error(`${value} is not a valid ${template.name}`)
}
await ctx.photon.attributes.create({
data: {
value,
dog: {
connect: {
id: dog.id
}
},
attributeTemplate: {
connect: {
id: template.id
}
},
}
})
const returnDog = await ctx.photon.dogs.findOne({
include: { attributes: true },
where: { id: dog.id }
});
if (!returnDog) {
throw new Error('this should be a 404')
}
return returnDog
}
})
// t.crud.createOneUser({ alias: 'signupUser' })
// t.crud.deleteOnePost()
// t.field('createDraft', {
// type: 'Post',
// args: {
// title: stringArg({ nullable: false }),
// content: stringArg(),
// authorEmail: stringArg(),
// },
// resolve: (_, { title, content, authorEmail }, ctx) => {
// return ctx.photon.posts.create({
// data: {
// title,
// content,
// published: false,
// author: {
// connect: { email: authorEmail },
// },
// },
// })
// },
// })
// t.field('publish', {
// type: 'Post',
// nullable: true,
// args: {
// id: idArg(),
// },
// resolve: (_, { id }, ctx) => {
// return ctx.photon.posts.update({
// where: { id },
// data: { published: true },
// })
// },
// })
},
})
export const schema = makeSchema({
types: [Query, Mutation, Dog, Attribute, AttributeTemplate],
plugins: [nexusPrismaPlugin()],
typegenAutoConfig: {
contextType: 'Context.Context',
sources: [
{
source: '@generated/photon',
alias: 'photon',
},
{
source: require.resolve('./context'),
alias: 'Context',
},
],
},
})