summaryrefslogtreecommitdiffstats
path: root/backend/src/resolvers/Mutation.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-04-05 13:54:22 -0400
committerWes Bos <wesbos@gmail.com>2018-04-05 13:54:22 -0400
commitf4f10d5eb690a33dd1c112358457a2b987297f26 (patch)
treed97326763dd016c13a068fb7dee40fc0b9667f83 /backend/src/resolvers/Mutation.js
parenta75b2491758a451283e2a373bb3fbe886520b345 (diff)
work
Diffstat (limited to 'backend/src/resolvers/Mutation.js')
-rw-r--r--backend/src/resolvers/Mutation.js14
1 files changed, 12 insertions, 2 deletions
diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js
index 9127b04..daaa7de 100644
--- a/backend/src/resolvers/Mutation.js
+++ b/backend/src/resolvers/Mutation.js
@@ -46,13 +46,16 @@ const mutations = {
// Creation of Post Mutations
async createItem(parent, args, ctx, info) {
- const userId = getUserId(ctx);
+ if (!ctx.request.userId) {
+ throw new Error('You must be logged in to create an item');
+ }
+
const item = await ctx.db.mutation.createItem(
{
data: {
user: {
connect: {
- id: userId,
+ id: ctx.request.userId,
},
},
...args,
@@ -80,7 +83,14 @@ const mutations = {
},
async updateItem(parent, args, ctx, info) {
+ const user = ctx.request.user;
+ const item = await ctx.db.query.item({ where: { id: args.id } }, `{ user { id } }`);
+ if (item.user.id !== user.id || !user.permissions.includes('ADMIN')) {
+ throw new Error('You are not allowed to update that item!');
+ }
+
const updates = { ...args };
+ // remove the ID because you can't update that
delete updates.id;
return ctx.db.mutation.updateItem({
where: { id: args.id },