1
1
import { prismaObjectType , objectType , mutationField , idArg } from 'yoga'
2
- import { PayloadInterface } from './PayloadInterface' ;
3
- import { authorizeUser } from './auth' ;
4
- import { Context } from '../context' ;
5
- import { UserTutorial as UserTutorialType , UserTutorialCreateInput } from '../../.yoga/prisma-client' ;
2
+ import { PayloadInterface } from './PayloadInterface'
3
+ import { authorizeUser } from './auth'
4
+ import { Context } from '../context'
5
+ import {
6
+ UserTutorial as UserTutorialType ,
7
+ UserTutorialCreateInput ,
8
+ } from '../../.yoga/prisma-client'
6
9
7
10
export const UserTutorial = prismaObjectType ( {
8
11
name : 'UserTutorial' ,
@@ -14,93 +17,108 @@ export const UserTutorial = prismaObjectType({
14
17
} )
15
18
16
19
export const UserTutorialPayload = objectType ( {
17
- name : " UserTutorialPayload" ,
18
- definition : ( type ) => {
19
- type . implements ( PayloadInterface ) ;
20
- type . field ( " userTutorial" , {
20
+ name : ' UserTutorialPayload' ,
21
+ definition : type => {
22
+ type . implements ( PayloadInterface )
23
+ type . field ( ' userTutorial' , {
21
24
type : UserTutorial ,
22
- nullable : true
23
- } ) ;
24
- }
25
+ nullable : true ,
26
+ } )
27
+ } ,
25
28
} )
26
29
27
- export const upvoteTutorial = mutationField ( " upvoteTutorial" , {
30
+ export const upvoteTutorial = mutationField ( ' upvoteTutorial' , {
28
31
type : UserTutorialPayload ,
29
- description : " An authenticated user can upvote a tutorial." ,
32
+ description : ' An authenticated user can upvote a tutorial.' ,
30
33
args : {
31
34
tutorialId : idArg ( {
32
- required : true
33
- } )
35
+ required : true ,
36
+ } ) ,
34
37
} ,
35
38
authorize : authorizeUser ( ) ,
36
39
resolve : async ( _ , { tutorialId } , ctx ) => {
37
- const userId = ctx . currentUserId ;
38
- const existingUserTutorial = await getUserTutorial ( {
39
- userId,
40
- tutorialId
41
- } , ctx ) ;
42
- let upsertedUserTutorial = await upsertUserTutorial ( {
43
- userId,
44
- tutorialId,
45
- userTutorialId : existingUserTutorial && existingUserTutorial . id ,
46
- updates : {
47
- upvoted : ! existingUserTutorial . upvoted ,
40
+ const userId = ctx . currentUserId
41
+ const existingUserTutorial = await getUserTutorial (
42
+ {
43
+ userId,
44
+ tutorialId,
45
+ } ,
46
+ ctx ,
47
+ )
48
+ let upsertedUserTutorial = await upsertUserTutorial (
49
+ {
50
+ userId,
51
+ tutorialId,
52
+ userTutorialId : existingUserTutorial && existingUserTutorial . id ,
53
+ updates : {
54
+ upvoted : existingUserTutorial ? ! existingUserTutorial . upvoted : true ,
55
+ } ,
48
56
} ,
49
- } , ctx ) ;
50
- return ( {
51
- code : "200" ,
57
+ ctx ,
58
+ )
59
+ return {
60
+ code : '200' ,
52
61
success : true ,
53
62
message : null ,
54
- userTutorial : upsertedUserTutorial
55
- } )
56
- }
63
+ userTutorial : upsertedUserTutorial ,
64
+ }
65
+ } ,
57
66
} )
58
67
59
- async function upsertUserTutorial ( args : { userTutorialId ?: string , updates : UserTutorialCreateInput , userId : string , tutorialId : any } , ctx : Context ) : Promise < UserTutorialType > {
60
- const { userTutorialId, updates, userId, tutorialId } = args ;
61
- let upsertedUserTutorial ;
68
+ async function upsertUserTutorial (
69
+ args : {
70
+ userTutorialId ?: string
71
+ updates : UserTutorialCreateInput
72
+ userId : string
73
+ tutorialId : any
74
+ } ,
75
+ ctx : Context ,
76
+ ) : Promise < UserTutorialType > {
77
+ const { userTutorialId, updates, userId, tutorialId } = args
78
+ let upsertedUserTutorial
62
79
if ( userTutorialId ) {
63
80
upsertedUserTutorial = await ctx . prisma . updateUserTutorial ( {
64
81
where : {
65
- id : userTutorialId
82
+ id : userTutorialId ,
66
83
} ,
67
- data : updates
68
- } ) ;
69
- }
70
- else {
84
+ data : updates ,
85
+ } )
86
+ } else {
71
87
upsertedUserTutorial = await ctx . prisma . createUserTutorial ( {
72
88
...updates ,
73
89
user : {
74
90
connect : {
75
- id : userId
76
- }
91
+ id : userId ,
92
+ } ,
77
93
} ,
78
94
tutorial : {
79
95
connect : {
80
- id : tutorialId
81
- }
82
- }
83
- } ) ;
96
+ id : tutorialId ,
97
+ } ,
98
+ } ,
99
+ } )
84
100
}
85
- return upsertedUserTutorial ;
101
+ return upsertedUserTutorial
86
102
}
87
103
88
- export async function getUserTutorial ( args : { userId : string , tutorialId : any } , ctx : Context ) : Promise < null | UserTutorialType > {
89
- const { userId, tutorialId } = args ;
104
+ export async function getUserTutorial (
105
+ args : { userId : string ; tutorialId : any } ,
106
+ ctx : Context ,
107
+ ) : Promise < null | UserTutorialType > {
108
+ const { userId, tutorialId } = args
90
109
const existingUserTutorials = await ctx . prisma . userTutorials ( {
91
110
first : 1 ,
92
111
where : {
93
112
user : {
94
- id : userId
113
+ id : userId ,
95
114
} ,
96
115
tutorial : {
97
- id : tutorialId
98
- }
99
- }
100
- } ) ;
116
+ id : tutorialId ,
117
+ } ,
118
+ } ,
119
+ } )
101
120
if ( existingUserTutorials . length > 0 ) {
102
- return existingUserTutorials [ 0 ] ;
121
+ return existingUserTutorials [ 0 ]
103
122
}
104
- return null ;
123
+ return null
105
124
}
106
-
0 commit comments