Skip to content

Commit e36262e

Browse files
committed
Add mutations
1 parent 5da5372 commit e36262e

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

graphQL/data/resolvers.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import crypto from "crypto"
2-
import { Widgets } from "./dbconnectors";
2+
import { Widgets } from "./dbConnectors.js";
33

44
// class Product {
55
// constructor(id, { name, description, price, soldout, stores }) {
@@ -23,12 +23,39 @@ const resolvers = {
2323
catch (e) {
2424
throw new Error(e)
2525
}
26-
return new Product(id, productDatabase[id])
2726
},
28-
createProduct: ({ input }) => {
29-
// let id = crypto.randomBytes(10).toString('hex');
30-
// productDatabase[id] = input;
31-
// return new Product(id, input);
27+
createProduct: async ({ input }) => {
28+
29+
const newWidget = new Widgets({
30+
name: input.name,
31+
description: input.description,
32+
price: input.price,
33+
soldout: input.soldout,
34+
stores: input.stores,
35+
})
36+
try {
37+
await newWidget.save()
38+
return newWidget
39+
} catch (e) {
40+
throw new Error(e)
41+
}
42+
},
43+
updateProduct: async ({input}) => {
44+
try {
45+
const updateWidgets = Widgets.findOneAndUpdate({_id:input.id}, input,
46+
{new: true})
47+
return updateWidgets
48+
} catch (e) {
49+
throw new Error(e)
50+
}
51+
},
52+
deleteProduct: async ({id}) => {
53+
try {
54+
await Widgets.deleteOne({_id: id})
55+
return "Successfully removed ... "
56+
} catch (e) {
57+
throw new Error(e)
58+
}
3259
}
3360
}
3461

graphQL/data/schema.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const schema = buildSchema(`
77
description: String
88
price: Float
99
soldout: Soldout
10-
stores: [Store]!
10+
stores: String
1111
}
1212
1313
enum Soldout {
@@ -22,22 +22,24 @@ const schema = buildSchema(`
2222
type Query {
2323
getProduct(id: ID): Product
2424
}
25-
25+
"""
2626
input StoreInput {
2727
store: String
2828
}
29-
29+
"""
3030
input ProductInput {
3131
id: ID
3232
name: String
3333
description: String
3434
price: Float
3535
soldout: Soldout
36-
stores: [StoreInput]!
36+
stores: String
3737
}
3838
3939
type Mutation {
4040
createProduct(input: ProductInput): Product
41+
updateProduct(input: ProductInput): Product
42+
deleteProduct(id: ID!): String
4143
}
4244
`)
4345

0 commit comments

Comments
 (0)