forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
235 lines (230 loc) · 6.18 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { list, graphQLSchemaExtension, gql, graphql } from '@keystone-next/keystone';
import {
text,
relationship,
checkbox,
password,
timestamp,
select,
virtual,
image,
file,
} from '@keystone-next/keystone/fields';
import { document } from '@keystone-next/fields-document';
// import { cloudinaryImage } from '@keystone-next/cloudinary';
import { KeystoneListsAPI } from '@keystone-next/keystone/types';
import { v4 } from 'uuid';
import { componentBlocks } from './admin/fieldViews/Content';
import { KeystoneListsTypeInfo } from '.keystone/types';
type AccessArgs = {
session?: {
itemId?: string;
listKey?: string;
data: {
name?: string;
isAdmin: boolean;
};
};
item?: any;
};
export const access = {
isAdmin: ({ session }: AccessArgs) => !!session?.data.isAdmin,
};
const randomNumber = () => Math.round(Math.random() * 10);
export const lists = {
User: list({
ui: {
listView: {
initialColumns: ['name', 'posts', 'avatar'],
},
},
fields: {
/** The user's first and last name. */
name: text({ validation: { isRequired: true } }),
/** Email is used to log into the system. */
email: text({ isIndexed: 'unique', validation: { isRequired: true } }),
/** Avatar upload for the users profile, stored locally */
avatar: image(),
attachment: file(),
/** Used to log in. */
password: password(),
/** Administrators have more access to various lists and fields. */
isAdmin: checkbox({
access: {
read: access.isAdmin,
create: access.isAdmin,
update: access.isAdmin,
},
ui: {
createView: {
fieldMode: args => (access.isAdmin(args) ? 'edit' : 'hidden'),
},
itemView: {
fieldMode: args => (access.isAdmin(args) ? 'edit' : 'read'),
},
},
}),
roles: text({}),
phoneNumbers: relationship({
ref: 'PhoneNumber.user',
many: true,
ui: {
// TODO: Work out how to use custom views to customise the card + edit / create forms
// views: './admin/fieldViews/user/phoneNumber',
displayMode: 'cards',
cardFields: ['type', 'value'],
inlineEdit: { fields: ['type', 'value'] },
inlineCreate: { fields: ['type', 'value'] },
linkToItem: true,
// removeMode: 'delete',
},
}),
posts: relationship({ ref: 'Post.author', many: true }),
randomNumber: virtual({
field: graphql.field({
type: graphql.Float,
resolve() {
return randomNumber();
},
}),
}),
},
}),
PhoneNumber: list({
ui: {
isHidden: true,
// parentRelationship: 'user',
},
fields: {
label: virtual({
field: graphql.field({
type: graphql.String,
resolve(item) {
return `${item.type} - ${item.value}`;
},
}),
ui: {
listView: {
fieldMode: 'hidden',
},
itemView: {
fieldMode: 'hidden',
},
},
}),
user: relationship({ ref: 'User.phoneNumbers' }),
type: select({
options: [
{ label: 'Home', value: 'home' },
{ label: 'Work', value: 'work' },
{ label: 'Mobile', value: 'mobile' },
],
ui: {
displayMode: 'segmented-control',
},
}),
value: text({}),
},
}),
Post: list({
fields: {
title: text(),
// TODO: expand this out into a proper example project
// Enable this line to test custom field views
// test: text({ ui: { views: require.resolve('./admin/fieldViews/Test.tsx') } }),
status: select({
options: [
{ label: 'Published', value: 'published' },
{ label: 'Draft', value: 'draft' },
],
ui: {
displayMode: 'segmented-control',
},
validation: {
isRequired: true,
},
defaultValue: 'draft',
}),
content: document({
ui: { views: require.resolve('./admin/fieldViews/Content.tsx') },
relationships: {
mention: {
kind: 'inline',
label: 'Mention',
listKey: 'User',
},
featuredAuthors: {
kind: 'prop',
listKey: 'User',
many: true,
selection: `posts(take: 10) {
title
}`,
},
},
formatting: true,
layouts: [
[1, 1],
[1, 1, 1],
[2, 1],
[1, 2],
[1, 2, 1],
],
links: true,
dividers: true,
componentBlocks,
}),
publishDate: timestamp(),
author: relationship({
ref: 'User.posts',
ui: {
displayMode: 'cards',
cardFields: ['name', 'email'],
inlineEdit: { fields: ['name', 'email'] },
linkToItem: true,
inlineCreate: { fields: ['name', 'email'] },
},
}),
},
}),
};
export const extendGraphqlSchema = graphQLSchemaExtension({
typeDefs: gql`
type Query {
randomNumber: RandomNumber
uuid: ID!
}
type RandomNumber {
number: Int
generatedAt: Int
}
type Mutation {
createRandomPosts: [Post!]!
}
`,
resolvers: {
RandomNumber: {
number(rootVal: { number: number }) {
return rootVal.number * 1000;
},
},
Mutation: {
createRandomPosts(root, args, context) {
// TODO: add a way to verify access control here, e.g
// await context.verifyAccessControl(userIsAdmin);
const data = Array.from({ length: 238 }).map((x, i) => ({ title: `Post ${i}` }));
// note this usage of the type is important because it tests that the generated
// KeystoneListsTypeInfo extends Record<string, BaseGeneratedListTypes>
const query = context.query as KeystoneListsAPI<KeystoneListsTypeInfo>;
return query.Post.createMany({ data });
},
},
Query: {
randomNumber: () => ({
number: randomNumber(),
generatedAt: Date.now(),
}),
uuid: () => v4(),
},
},
});