summaryrefslogtreecommitdiffstats
path: root/backend/src/resolvers
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-01-30 12:41:01 -0500
committerWes Bos <wesbos@gmail.com>2018-01-30 12:41:01 -0500
commitcd0b07b3adb36fb5ec098f9e511ab45d774fee7b (patch)
treea203e5010219ccea1acc6bbd2c0a4bcfa0a78615 /backend/src/resolvers
parent0a1648bf47490b312169c531aeb51ef4725e8d2e (diff)
Move to Prisma Backend
Diffstat (limited to 'backend/src/resolvers')
-rw-r--r--backend/src/resolvers/AuthPayload.js7
-rw-r--r--backend/src/resolvers/Mutation.js104
-rw-r--r--backend/src/resolvers/Query.js31
3 files changed, 142 insertions, 0 deletions
diff --git a/backend/src/resolvers/AuthPayload.js b/backend/src/resolvers/AuthPayload.js
new file mode 100644
index 0000000..f5945b7
--- /dev/null
+++ b/backend/src/resolvers/AuthPayload.js
@@ -0,0 +1,7 @@
+const { Context } = require('../utils');
+
+const AuthPayload = {
+ user: async ({ user: { id } }, args, ctx, info) => ctx.db.query.user({ where: { id } }, info),
+};
+
+module.exports = AuthPayload;
diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js
new file mode 100644
index 0000000..2796cf8
--- /dev/null
+++ b/backend/src/resolvers/Mutation.js
@@ -0,0 +1,104 @@
+const bcrypt = require('bcryptjs');
+const jwt = require('jsonwebtoken');
+const { forwardTo } = require('prisma-binding');
+const { getUserId, Context } = require('../utils');
+
+const mutations = {
+ // Signup Mutations
+ async signup(parent, args, ctx, info) {
+ const password = await bcrypt.hash(args.password, 10);
+ const user = await ctx.db.mutation.createUser({
+ data: { ...args, password },
+ });
+
+ return {
+ token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
+ user,
+ };
+ },
+
+ async login(parent, { email, password }, ctx, info) {
+ const user = await ctx.db.query.user({ where: { email } });
+ if (!user) {
+ throw new Error(`No such user found for email: ${email}`);
+ }
+
+ const valid = await bcrypt.compare(password, user.password);
+ if (!valid) {
+ throw new Error('Invalid password');
+ }
+
+ return {
+ token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
+ user,
+ };
+ },
+
+ // Creation of Post Mutations
+ createItem: forwardTo('db'),
+ deleteItem: forwardTo('db'),
+ async createDraft(parent, { title, text }, ctx, info) {
+ // const userId = getUserId(ctx);
+ const userId = getUserId(ctx);
+ return ctx.db.mutation.createPost(
+ {
+ data: {
+ title,
+ text,
+ isPublished: false,
+ author: {
+ connect: { id: userId },
+ },
+ },
+ },
+ info
+ );
+ },
+
+ async publish(parent, { id }, ctx, info) {
+ const userId = getUserId(ctx);
+ const postExists = await ctx.db.exists.Post({
+ id,
+ author: { id: userId },
+ });
+ if (!postExists) {
+ throw new Error(`Post not found or you're not the author`);
+ }
+
+ return ctx.db.mutation.updatePost(
+ {
+ where: { id },
+ data: { isPublished: true },
+ },
+ info
+ );
+ },
+
+ async deletePost(parent, { id }, ctx, info) {
+ const userId = getUserId(ctx);
+ const postExists = await ctx.db.exists.Post({
+ id,
+ author: { id: userId },
+ });
+ if (!postExists) {
+ throw new Error(`Post not found or you're not the author`);
+ }
+
+ return ctx.db.mutation.deletePost({ where: { id } });
+ },
+
+ // Wes Added this brand new one!
+ async updatePost(parent, args, ctx, info) {
+ const updatedPost = await ctx.db.mutation.updatePost({
+ where: { id: args.id },
+ data: {
+ title: args.title,
+ text: args.text,
+ },
+ });
+ console.log(updatedPost);
+ return updatedPost;
+ },
+};
+
+module.exports = mutations;
diff --git a/backend/src/resolvers/Query.js b/backend/src/resolvers/Query.js
new file mode 100644
index 0000000..9d9d142
--- /dev/null
+++ b/backend/src/resolvers/Query.js
@@ -0,0 +1,31 @@
+const { getUserId, Context } = require('../utils');
+
+const Query = {
+ feed(parent, args, ctx, info) {
+ return ctx.db.query.posts({ where: { isPublished: true } }, info);
+ },
+
+ drafts(parent, args, ctx, info) {
+ const id = getUserId(ctx);
+
+ const where = {
+ isPublished: false,
+ author: {
+ id,
+ },
+ };
+
+ return ctx.db.query.posts({ where }, info);
+ },
+
+ post(parent, { id }, ctx, info) {
+ return ctx.db.query.post({ where: { id } }, info);
+ },
+
+ me(parent, args, ctx, info) {
+ const id = getUserId(ctx);
+ return ctx.db.query.user({ where: { id } }, info);
+ },
+};
+
+module.exports = Query;