@@ -30,7 +30,7 @@ export default async function Home() {
30
30
throw new Error ( "Failed to get blog post collection id" ) ;
31
31
}
32
32
try {
33
- const data = await basehub ( { draft : true , token : getToken ( ) } ) . raw ( {
33
+ const data = await basehub ( { token : await getToken ( ) } ) . raw ( {
34
34
query : `{ blogPosts { _id } }` ,
35
35
} ) ;
36
36
// @ts -ignore
@@ -41,38 +41,66 @@ export default async function Home() {
41
41
return dostuff ( retryCount ++ ) ;
42
42
} else {
43
43
// means the collection doesn't exist. we'll create it.
44
- const result = await basehub ( { token : getToken ( ) } ) . mutation ( {
44
+ const result = await basehub ( { token : await getToken ( ) } ) . mutation ( {
45
45
transaction : {
46
46
__args : {
47
- data : {
48
- type : "create" ,
49
- data : {
50
- type : "collection" ,
51
- title : "Blog Posts" ,
52
- template : [
53
- {
54
- type : "text" ,
55
- title : "Excerpt" ,
56
- isRequired : true ,
57
- } ,
58
- {
59
- type : "date" ,
60
- title : "Date" ,
61
- isRequired : true ,
62
- } ,
63
- {
64
- type : "image" ,
65
- title : "Cover Image" ,
66
- } ,
67
- {
68
- type : "rich-text" ,
69
- title : "Body" ,
70
- isRequired : true ,
71
- formatting : "all" ,
72
- } ,
73
- ] ,
47
+ data : [
48
+ {
49
+ type : "create" ,
50
+ data : {
51
+ type : "collection" ,
52
+ title : "Authors" ,
53
+ transactionId : "authors" ,
54
+ template : [
55
+ {
56
+ type : "text" ,
57
+ title : "Role" ,
58
+ } ,
59
+ {
60
+ type : "image" ,
61
+ title : "Avatar" ,
62
+ } ,
63
+ ] ,
64
+ } ,
74
65
} ,
75
- } ,
66
+ {
67
+ type : "create" ,
68
+ data : {
69
+ type : "collection" ,
70
+ title : "Blog Posts" ,
71
+ template : [
72
+ {
73
+ type : "text" ,
74
+ title : "Excerpt" ,
75
+ isRequired : true ,
76
+ } ,
77
+ {
78
+ type : "date" ,
79
+ title : "Date" ,
80
+ isRequired : true ,
81
+ } ,
82
+ {
83
+ type : "image" ,
84
+ title : "Cover Image" ,
85
+ } ,
86
+ {
87
+ type : "reference" ,
88
+ title : "Author(s)" ,
89
+ apiName : "authors" ,
90
+ allowedComponents : [ "authors" ] ,
91
+ isRequired : true ,
92
+ multiple : true ,
93
+ } ,
94
+ {
95
+ type : "rich-text" ,
96
+ title : "Body" ,
97
+ isRequired : true ,
98
+ formatting : "all" ,
99
+ } ,
100
+ ] ,
101
+ } ,
102
+ } ,
103
+ ] ,
76
104
} ,
77
105
message : true ,
78
106
status : true ,
@@ -201,12 +229,12 @@ export default async function Home() {
201
229
} }
202
230
</ Pump >
203
231
< APITokenForm
204
- defaultValue = { getToken ( ) }
232
+ defaultValue = { await getToken ( ) }
205
233
action = { async ( data ) => {
206
234
"use server" ;
207
235
const token = data . get ( "token" ) ;
208
236
if ( typeof token === "string" ) {
209
- cookies ( ) . set ( "basehub-admin-token" , token ) ;
237
+ ( await cookies ( ) ) . set ( "basehub-admin-token" , token ) ;
210
238
}
211
239
} }
212
240
/>
@@ -217,8 +245,9 @@ export default async function Home() {
217
245
"use server" ;
218
246
219
247
const collectionId = await getBlogPostCollectionId ( ) ;
248
+ const authorName = faker . person . firstName ( ) ;
220
249
221
- const result = await basehub ( { token : getToken ( ) } ) . mutation ( {
250
+ const result = await basehub ( { token : await getToken ( ) } ) . mutation ( {
222
251
transaction : {
223
252
__args : {
224
253
autoCommit : "Create a blog post with random data" ,
@@ -238,6 +267,29 @@ export default async function Home() {
238
267
type : "date" ,
239
268
value : new Date ( ) . toISOString ( ) ,
240
269
} ,
270
+ authors : {
271
+ type : "reference" ,
272
+ value : {
273
+ idempotency : {
274
+ key : "title" ,
275
+ value : authorName ,
276
+ } ,
277
+ type : "instance" ,
278
+ title : authorName ,
279
+ value : {
280
+ role : {
281
+ type : "text" ,
282
+ value : faker . person . jobTitle ( ) ,
283
+ } ,
284
+ avatar : {
285
+ type : "image" ,
286
+ value : {
287
+ url : faker . image . avatar ( ) ,
288
+ } ,
289
+ } ,
290
+ } ,
291
+ } ,
292
+ } ,
241
293
body : {
242
294
type : "rich-text" ,
243
295
value : {
@@ -280,7 +332,7 @@ export default async function Home() {
280
332
const id = formData . get ( "id" ) ;
281
333
if ( typeof id !== "string" ) throw new Error ( "Invalid ID" ) ;
282
334
283
- const result = await basehub ( { token : getToken ( ) } ) . mutation ( {
335
+ const result = await basehub ( { token : await getToken ( ) } ) . mutation ( {
284
336
transaction : {
285
337
__args : {
286
338
autoCommit : "Delete a blog post" ,
@@ -320,7 +372,7 @@ export default async function Home() {
320
372
let imageURL : string | undefined ;
321
373
let imageFileName : string | undefined ;
322
374
if ( image && typeof image === "object" && image . size > 0 ) {
323
- const result = await basehub ( { token : getToken ( ) } ) . mutation ( {
375
+ const result = await basehub ( { token : await getToken ( ) } ) . mutation ( {
324
376
getUploadSignedURL : {
325
377
__args : {
326
378
fileName : image . name ,
@@ -373,7 +425,7 @@ export default async function Home() {
373
425
} ) ,
374
426
) ;
375
427
376
- const result = await basehub ( { token : getToken ( ) } ) . mutation ( {
428
+ const result = await basehub ( { token : await getToken ( ) } ) . mutation ( {
377
429
transaction : {
378
430
__args : {
379
431
autoCommit : "Update a blog post" ,
@@ -525,8 +577,8 @@ const Form = ({
525
577
) ;
526
578
} ;
527
579
528
- function getToken ( ) {
529
- const tokenFromCookie = cookies ( ) . get ( "basehub-admin-token" ) ;
580
+ async function getToken ( ) {
581
+ const tokenFromCookie = ( await cookies ( ) ) . get ( "basehub-admin-token" ) ;
530
582
if ( tokenFromCookie ) return tokenFromCookie . value ;
531
583
532
584
// will fallback to env vars
0 commit comments