summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/35/backend
diff options
context:
space:
mode:
Diffstat (limited to 'stepped-solutions/35/backend')
-rwxr-xr-xstepped-solutions/35/backend/src/index.js46
-rwxr-xr-xstepped-solutions/35/backend/src/resolvers/Query.js34
-rwxr-xr-xstepped-solutions/35/backend/src/schema.graphql31
3 files changed, 111 insertions, 0 deletions
diff --git a/stepped-solutions/35/backend/src/index.js b/stepped-solutions/35/backend/src/index.js
new file mode 100755
index 0000000..804d6d6
--- /dev/null
+++ b/stepped-solutions/35/backend/src/index.js
@@ -0,0 +1,46 @@
+const cookieParser = require('cookie-parser');
+const jwt = require('jsonwebtoken');
+
+require('dotenv').config({ path: 'variables.env' });
+const createServer = require('./createServer');
+const db = require('./db');
+
+const server = createServer();
+
+server.express.use(cookieParser());
+
+// decode the JWT so we can get the user Id on each request
+server.express.use((req, res, next) => {
+ const { token } = req.cookies;
+ if (token) {
+ const { userId } = jwt.verify(token, process.env.APP_SECRET);
+ // put the userId onto the req for future requests to access
+ req.userId = userId;
+ }
+ next();
+});
+
+// 2. Create a middleware that populates the user on each request
+
+server.express.use(async (req, res, next) => {
+ // if they aren't logged in, skip this
+ if (!req.userId) return next();
+ const user = await db.query.user(
+ { where: { id: req.userId } },
+ '{ id, permissions, email, name }'
+ );
+ req.user = user;
+ next();
+});
+
+server.start(
+ {
+ cors: {
+ credentials: true,
+ origin: process.env.FRONTEND_URL,
+ },
+ },
+ deets => {
+ console.log(`Server is now running on port http://localhost:${deets.port}`);
+ }
+);
diff --git a/stepped-solutions/35/backend/src/resolvers/Query.js b/stepped-solutions/35/backend/src/resolvers/Query.js
new file mode 100755
index 0000000..8af7b6c
--- /dev/null
+++ b/stepped-solutions/35/backend/src/resolvers/Query.js
@@ -0,0 +1,34 @@
+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);
+ },
+};
+
+module.exports = Query;
diff --git a/stepped-solutions/35/backend/src/schema.graphql b/stepped-solutions/35/backend/src/schema.graphql
new file mode 100755
index 0000000..323b1dc
--- /dev/null
+++ b/stepped-solutions/35/backend/src/schema.graphql
@@ -0,0 +1,31 @@
+# import * from './generated/prisma.graphql'
+
+type SuccessMessage {
+ message: String
+}
+
+type Mutation {
+ createItem(title: String, description: String, price: Int, image: String, largeImage: String): Item!
+ updateItem(id: ID!, title: String, description: String, price: Int): Item!
+ deleteItem(id: ID!): Item
+ signup(email: String!, password: String!, name: String!): User!
+ signin(email: String!, password: String!): User!
+ signout: SuccessMessage
+ requestReset(email: String!): SuccessMessage
+ resetPassword(resetToken: String!, password: String!, confirmPassword: String!): User!
+}
+
+type Query {
+ items(where: ItemWhereInput, orderBy: ItemOrderByInput, skip: Int, first: Int): [Item]!
+ item(where: ItemWhereUniqueInput!): Item
+ itemsConnection(where: ItemWhereInput): ItemConnection!
+ me: User
+ users: [User]!
+}
+
+type User{
+ id: ID!
+ name: String!
+ email: String!
+ permissions: [Permission!]!
+}