summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/53/backend/src/resolvers/Query.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-09-07 12:15:53 -0400
committerWes Bos <wesbos@gmail.com>2018-09-07 12:15:53 -0400
commit093c172d16732ba8d01faa313e0af99d26902205 (patch)
tree84838c4f6410b43f42b56219190a62e14e072ef3 /stepped-solutions/53/backend/src/resolvers/Query.js
parent9876e7dfca6fa2bbf6ac383eeb8e2c82e05c2164 (diff)
getting there
Diffstat (limited to 'stepped-solutions/53/backend/src/resolvers/Query.js')
-rwxr-xr-xstepped-solutions/53/backend/src/resolvers/Query.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/stepped-solutions/53/backend/src/resolvers/Query.js b/stepped-solutions/53/backend/src/resolvers/Query.js
new file mode 100755
index 0000000..8e0f169
--- /dev/null
+++ b/stepped-solutions/53/backend/src/resolvers/Query.js
@@ -0,0 +1,55 @@
+const { forwardTo } = require('prisma-binding');
+const { hasPermission } = require('../utils');
+
+const Query = {
+ items: forwardTo('db'),
+ item: forwardTo('db'),
+ itemsConnection: forwardTo('db'),
+ me(parent, args, ctx, info) {
+ // check if there is a current user ID
+ if (!ctx.request.userId) {
+ return null;
+ }
+ return ctx.db.query.user(
+ {
+ where: { id: ctx.request.userId },
+ },
+ info
+ );
+ },
+ async users(parent, args, ctx, info) {
+ // 1. Check if they are logged in
+ if (!ctx.request.userId) {
+ throw new Error('You must be logged in!');
+ }
+ console.log(ctx.request.userId);
+ // 2. Check if the user has the permissions to query all the users
+ hasPermission(ctx.request.user, ['ADMIN', 'PERMISSIONUPDATE']);
+
+ // 2. if they do, query all the users!
+ return ctx.db.query.users({}, info);
+ },
+ async order(parent, args, ctx, info) {
+ // 1. Make sure they are logged in
+ if (!ctx.request.userId) {
+ throw new Error('You arent logged in!');
+ }
+ // 2. Query the current order
+ const order = await ctx.db.query.order(
+ {
+ where: { id: args.id },
+ },
+ info
+ );
+ // 3. Check if the have the permissions to see this order
+ const ownsOrder = order.user.id === ctx.request.userId;
+ const hasPermissionToSeeOrder = ctx.request.user.permissions.includes('ADMIN');
+ if (!ownsOrder || !hasPermission) {
+ throw new Error('You cant see this buddd');
+ }
+ // 4. Return the order
+ return order;
+ },
+};
+
+module.exports = Query;