summaryrefslogtreecommitdiffstats
path: root/backend/src/resolvers/Query.js
blob: 22dcd2af3c4593d6a9ac7ee5c457e4e8477e38a9 (plain)
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
const { getUserId, Context, checkForUserId } = require('../utils');
const { forwardTo } = require('prisma-binding');

const Query = {
  items(parent, args, ctx, info) {
    console.log('ITEMS!');
    // check auth
    return ctx.db.query.items({ ...args }, info);
  },

  itemsConnection: forwardTo('db'),

  // TODO: Make sure they own this order before looking it up
  order: forwardTo('db'),

  me(parent, args, ctx, info) {
    const Authorization = ctx.request.get('Authorization');
    if (!Authorization || Authorization === 'null') {
      console.log('Authorization is null');
      return null;
    }
    const id = getUserId(ctx);
    return ctx.db.query.user({ where: { id } }, info);
  },

  async orders(parent, args, ctx, info) {
    const { userId } = ctx.request;
    if (!userId) {
      throw new Error('You must be signed in to see your orders');
    }
    return ctx.db.query.orders(
      {
        where: {
          user: { id: userId },
        },
      },
      info
    );
  },

  async users(parent, args, ctx, info) {
    // TODO Permissions
    const userId = getUserId(ctx);
    return ctx.db.query.users({}, info);
  },
};

module.exports = Query;