-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolvers.js
More file actions
61 lines (58 loc) · 1.79 KB
/
resolvers.js
File metadata and controls
61 lines (58 loc) · 1.79 KB
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
import { Items } from './db.js';
const resolvers = {
getItem: async ({ id }) => {
try {
return await Items.findById(id);
} catch (error) {
throw new Error(`Error fetching item with id ${id}: ${error.message}`);
}
},
getAllItems: async () => {
try {
return await Items.find({});
} catch (error) {
throw new Error(`Error fetching all items: ${error.message}`);
}
},
createItem: async ({ input }) => {
const newItem = new Items({
id: input.id,
name: input.name,
description: input.description,
status: input.status,
location: input.location,
metadata: input.metadata
});
newItem.id = newItem._id;
try {
await newItem.save();
return newItem;
} catch (error) {
throw new Error(`Error creating item: ${error.message}`);
}
},
updateItem: async ({ input }) => {
try {
return await Items.findOneAndUpdate({ _id: input.id }, input, { new: true });
} catch (error) {
throw new Error(`Error updating item with id ${input.id}: ${error.message}`);
}
},
deleteItem: async ({ id }) => {
try {
await Items.deleteOne({ _id: id });
return 'Successfully deleted item';
} catch (error) {
throw new Error(`Error deleting item with id ${id}: ${error.message}`);
}
},
deleteAllItems: async () => {
try {
await Items.collection.drop();
return 'Successfully deleted all items';
} catch (error) {
throw new Error(`Error deleting all items: ${error.message}`);
}
}
};
export default resolvers;