Skip to content

Commit 0bb7cc4

Browse files
committed
create upsert mutation
1 parent 860f439 commit 0bb7cc4

File tree

8 files changed

+92
-53
lines changed

8 files changed

+92
-53
lines changed

packages/server/.yoga/nexus-prisma/datamodel-info.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
uniqueFieldsByModel: {
88
User: ['id', 'email', 'githubHandle', 'githubUserId', 'expertise'],
99
UserTutorial: ['id'],
10-
Tutorial: ['id']
10+
Tutorial: ['id', 'gatsbyID']
1111
},
1212
embeddedTypes: [],
1313
clientPath: '.yoga/prisma-client',
@@ -5156,6 +5156,16 @@ export default {
51565156
"ofType": null
51575157
},
51585158
"defaultValue": null
5159+
},
5160+
{
5161+
"name": "gatsbyID",
5162+
"description": null,
5163+
"type": {
5164+
"kind": "SCALAR",
5165+
"name": "String",
5166+
"ofType": null
5167+
},
5168+
"defaultValue": null
51595169
}
51605170
],
51615171
"interfaces": null,

packages/server/.yoga/nexus-prisma/nexus-prisma.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,10 +2630,12 @@ export type UserTutorialWhereUniqueInputInputObject =
26302630

26312631
export interface TutorialWhereUniqueInput {
26322632
id?: string | null
2633+
gatsbyID?: string | null
26332634
}
26342635
export type TutorialWhereUniqueInputInputObject =
26352636
| Extract<keyof TutorialWhereUniqueInput, string>
26362637
| { name: 'id', alias?: string }
2638+
| { name: 'gatsbyID', alias?: string }
26372639

26382640
export interface UserCreateInput {
26392641
name?: string

packages/server/.yoga/nexus.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ export interface NexusGenInputs {
7373
numberofChapters_lte?: number | null; // Int
7474
numberofChapters_not?: number | null; // Int
7575
numberofChapters_not_in?: number[] | null; // [Int!]
76+
numberofStudents?: number | null; // Int
77+
numberofStudents_gt?: number | null; // Int
78+
numberofStudents_gte?: number | null; // Int
79+
numberofStudents_in?: number[] | null; // [Int!]
80+
numberofStudents_lt?: number | null; // Int
81+
numberofStudents_lte?: number | null; // Int
82+
numberofStudents_not?: number | null; // Int
83+
numberofStudents_not_in?: number[] | null; // [Int!]
7684
OR?: NexusGenInputs['TutorialWhereInput'][] | null; // [TutorialWhereInput!]
7785
updatedAt?: any | null; // DateTime
7886
updatedAt_gt?: any | null; // DateTime
@@ -82,6 +90,14 @@ export interface NexusGenInputs {
8290
updatedAt_lte?: any | null; // DateTime
8391
updatedAt_not?: any | null; // DateTime
8492
updatedAt_not_in?: any[] | null; // [DateTime!]
93+
upvotes?: number | null; // Int
94+
upvotes_gt?: number | null; // Int
95+
upvotes_gte?: number | null; // Int
96+
upvotes_in?: number[] | null; // [Int!]
97+
upvotes_lt?: number | null; // Int
98+
upvotes_lte?: number | null; // Int
99+
upvotes_not?: number | null; // Int
100+
upvotes_not_in?: number[] | null; // [Int!]
85101
userTutorials_every?: NexusGenInputs['UserTutorialWhereInput'] | null; // UserTutorialWhereInput
86102
userTutorials_none?: NexusGenInputs['UserTutorialWhereInput'] | null; // UserTutorialWhereInput
87103
userTutorials_some?: NexusGenInputs['UserTutorialWhereInput'] | null; // UserTutorialWhereInput
@@ -327,8 +343,8 @@ export interface NexusGenFieldTypes {
327343
}
328344
Mutation: { // field return type
329345
authenticate: NexusGenRootTypes['AuthenticateUserPayload'] | null; // AuthenticateUserPayload
330-
saveTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
331346
createTutorial: NexusGenRootTypes['Tutorial']; // Tutorial!
347+
saveTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
332348
upvoteTutorial: NexusGenRootTypes['UserTutorialPayload']; // UserTutorialPayload!
333349
}
334350
Query: { // field return type
@@ -342,6 +358,7 @@ export interface NexusGenFieldTypes {
342358
id: string; // ID!
343359
name: string; // String!
344360
numberofChapters: number; // Int!
361+
numberofStudents: number; // Int!
345362
numberOfStudents: number; // Int!
346363
updatedAt: any; // DateTime!
347364
upvotes: number; // Int!
@@ -394,13 +411,14 @@ export interface NexusGenArgTypes {
394411
authenticate: { // args
395412
githubCode: string; // String!
396413
}
397-
saveTutorial: { // args
398-
tutorialId: string; // ID!
399414
createTutorial: { // args
400415
gatsbyID: string; // ID!
401416
name: string; // String!
402417
numberofChapters: number; // Int!
403418
}
419+
saveTutorial: { // args
420+
tutorialId: string; // ID!
421+
}
404422
upvoteTutorial: { // args
405423
tutorialId: string; // ID!
406424
}

packages/server/.yoga/prisma-client/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ export type MutationType = "CREATED" | "UPDATED" | "DELETED";
245245

246246
export type TutorialWhereUniqueInput = AtLeastOne<{
247247
id: Maybe<ID_Input>;
248+
gatsbyID?: Maybe<String>;
248249
}>;
249250

250251
export interface UserTutorialWhereInput {
@@ -1542,6 +1543,11 @@ The `ID` scalar type represents a unique identifier, often used to refetch an ob
15421543
export type ID_Input = string | number;
15431544
export type ID_Output = string;
15441545

1546+
/*
1547+
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
1548+
*/
1549+
export type String = string;
1550+
15451551
/*
15461552
DateTime scalar input type, allowing Date
15471553
*/
@@ -1552,11 +1558,6 @@ DateTime scalar output type, which is always a string
15521558
*/
15531559
export type DateTimeOutput = string;
15541560

1555-
/*
1556-
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
1557-
*/
1558-
export type String = string;
1559-
15601561
/*
15611562
The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
15621563
*/

packages/server/.yoga/prisma-client/prisma-schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ input TutorialWhereInput {
304304
305305
input TutorialWhereUniqueInput {
306306
id: ID
307+
gatsbyID: String
307308
}
308309
309310
type User {

packages/server/prisma/datamodel.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Tutorial {
3333
createdAt: DateTime!
3434
updatedAt: DateTime!
3535
name: String!
36-
gatsbyID: String!
36+
gatsbyID: String! @unique
3737
numberofChapters: Int!
3838
numberofStudents: Int! @default(value: 0)
3939
upvotes: Int! @default(value: 0)

packages/server/src/graphql/Tutorial.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const tutorials = queryField('tutorials', {
8686
},
8787
})
8888

89-
export const createTutorial = mutationField('createTutorial', {
89+
export const upsertTutorial = mutationField('createTutorial', {
9090
type: Tutorial,
9191
description: 'Create tutorials from the MDX files',
9292
args: {
@@ -101,6 +101,26 @@ export const createTutorial = mutationField('createTutorial', {
101101
}),
102102
},
103103
resolve: async (_, { gatsbyID, name, numberofChapters }, ctx) => {
104-
return await ctx.prisma.createTutorial({ gatsbyID, name, numberofChapters })
104+
const existingTutorial = await ctx.prisma.tutorials({
105+
where: {
106+
gatsbyID: gatsbyID,
107+
},
108+
})
109+
let upsertedTutorial
110+
if (existingTutorial.length) {
111+
upsertedTutorial = await ctx.prisma.updateTutorial({
112+
where: {
113+
gatsbyID: gatsbyID,
114+
},
115+
data: { gatsbyID, name, numberofChapters },
116+
})
117+
} else {
118+
upsertedTutorial = await ctx.prisma.createTutorial({
119+
gatsbyID,
120+
name,
121+
numberofChapters,
122+
})
123+
}
124+
return upsertedTutorial
105125
},
106126
})

packages/server/src/schema.graphql

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### This file was autogenerated by Nexus 0.11.6
22
### Do not make changes to this file directly
33

4+
45
type AuthenticateUserPayload implements PayloadInterface {
56
code: String
67
message: String
@@ -13,34 +14,21 @@ scalar DateTime
1314

1415
type Mutation {
1516
authenticate(
16-
"""
17-
GitHub OAuth Token from the client.
18-
"""
17+
"""GitHub OAuth Token from the client."""
1918
githubCode: String!
2019
): AuthenticateUserPayload
2120

22-
"""
23-
An authenticated user can save a tutorial.
24-
"""
21+
"""Create tutorials from the MDX files"""
22+
createTutorial(gatsbyID: ID!, name: String!, numberofChapters: Int!): Tutorial!
23+
24+
"""An authenticated user can save a tutorial."""
2525
saveTutorial(tutorialId: ID!): UserTutorialPayload!
26-
"""
27-
Create tutorials from the MDX files
28-
"""
29-
createTutorial(
30-
gatsbyID: ID!
31-
name: String!
32-
numberofChapters: Int!
33-
): Tutorial!
3426

35-
"""
36-
An authenticated user can upvote a tutorial.
37-
"""
27+
"""An authenticated user can upvote a tutorial."""
3828
upvoteTutorial(tutorialId: ID!): UserTutorialPayload!
3929
}
4030

41-
"""
42-
The standard interface for all mutation responses
43-
"""
31+
"""The standard interface for all mutation responses"""
4432
interface PayloadInterface {
4533
code: String
4634
message: String
@@ -59,22 +47,13 @@ type Tutorial {
5947
id: ID!
6048
name: String!
6149
numberofChapters: Int!
50+
numberofStudents: Int!
6251
numberOfStudents: Int!
6352
updatedAt: DateTime!
6453
upvotes: Int!
65-
userTutorials(
66-
after: String
67-
before: String
68-
first: Int
69-
last: Int
70-
orderBy: UserTutorialOrderByInput
71-
skip: Int
72-
where: UserTutorialWhereInput
73-
): [UserTutorial!]
54+
userTutorials(after: String, before: String, first: Int, last: Int, orderBy: UserTutorialOrderByInput, skip: Int, where: UserTutorialWhereInput): [UserTutorial!]
7455

75-
"""
76-
The UserTutorial for the current user associated with this Tutorial.
77-
"""
56+
"""The UserTutorial for the current user associated with this Tutorial."""
7857
viewerUserTutorial: UserTutorial!
7958
}
8059

@@ -139,6 +118,14 @@ input TutorialWhereInput {
139118
numberofChapters_lte: Int
140119
numberofChapters_not: Int
141120
numberofChapters_not_in: [Int!]
121+
numberofStudents: Int
122+
numberofStudents_gt: Int
123+
numberofStudents_gte: Int
124+
numberofStudents_in: [Int!]
125+
numberofStudents_lt: Int
126+
numberofStudents_lte: Int
127+
numberofStudents_not: Int
128+
numberofStudents_not_in: [Int!]
142129
OR: [TutorialWhereInput!]
143130
updatedAt: DateTime
144131
updatedAt_gt: DateTime
@@ -148,6 +135,14 @@ input TutorialWhereInput {
148135
updatedAt_lte: DateTime
149136
updatedAt_not: DateTime
150137
updatedAt_not_in: [DateTime!]
138+
upvotes: Int
139+
upvotes_gt: Int
140+
upvotes_gte: Int
141+
upvotes_in: [Int!]
142+
upvotes_lt: Int
143+
upvotes_lte: Int
144+
upvotes_not: Int
145+
upvotes_not_in: [Int!]
151146
userTutorials_every: UserTutorialWhereInput
152147
userTutorials_none: UserTutorialWhereInput
153148
userTutorials_some: UserTutorialWhereInput
@@ -165,15 +160,7 @@ type User {
165160
id: ID!
166161
name: String!
167162
updatedAt: DateTime!
168-
userTutorials(
169-
after: String
170-
before: String
171-
first: Int
172-
last: Int
173-
orderBy: UserTutorialOrderByInput
174-
skip: Int
175-
where: UserTutorialWhereInput
176-
): [UserTutorial!]
163+
userTutorials(after: String, before: String, first: Int, last: Int, orderBy: UserTutorialOrderByInput, skip: Int, where: UserTutorialWhereInput): [UserTutorial!]
177164
}
178165

179166
type UserTutorial {

0 commit comments

Comments
 (0)