Started with Mongo
This commit is contained in:
16
server/src/resolvers/Appointment.js
Normal file
16
server/src/resolvers/Appointment.js
Normal file
@@ -0,0 +1,16 @@
|
||||
function createdBy(parent, args, context) {
|
||||
return context.mongo.appointment
|
||||
.findUnique({ where: { id: parent.id } })
|
||||
.createdBy();
|
||||
}
|
||||
|
||||
function follows(parent, args, context) {
|
||||
return context.mongo.appointment
|
||||
.findUnique({ where: { id: parent.id } })
|
||||
.follows();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createdBy,
|
||||
follows
|
||||
};
|
||||
16
server/src/resolvers/Follow.js
Normal file
16
server/src/resolvers/Follow.js
Normal file
@@ -0,0 +1,16 @@
|
||||
function appointment(parent, args, context) {
|
||||
return context.mongo.follow
|
||||
.findUnique({ where: { id: parent.id } })
|
||||
.appointment();
|
||||
}
|
||||
|
||||
function user(parent, args, context) {
|
||||
return context.mongo.follow
|
||||
.findUnique({ where: { id: parent.id } })
|
||||
.user();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
appointment,
|
||||
user
|
||||
};
|
||||
91
server/src/resolvers/Mutation.js
Normal file
91
server/src/resolvers/Mutation.js
Normal file
@@ -0,0 +1,91 @@
|
||||
const bcrypt = require('bcryptjs');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { APP_SECRET } = require('../utils');
|
||||
|
||||
function createAppointment(parent, args, context, info) {
|
||||
const { userId } = context;
|
||||
|
||||
const newAppointment = context.mongo.appointment.create({
|
||||
data: {
|
||||
title: args.title,
|
||||
description: args.description,
|
||||
createdBy: { connect: { id: userId } }
|
||||
}
|
||||
});
|
||||
|
||||
return newAppointment;
|
||||
}
|
||||
|
||||
async function signup(parent, args, context, info) {
|
||||
console.log(context);
|
||||
const password = await bcrypt.hash(args.password, 10);
|
||||
const user = await context.mongo.user.create({
|
||||
data: { ...args, password }
|
||||
});
|
||||
|
||||
const token = jwt.sign({ userId: user.id }, APP_SECRET);
|
||||
|
||||
return {
|
||||
token,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
async function login(parent, args, context, info) {
|
||||
const user = await context.mongo.user.findUnique({
|
||||
where: { email: args.email }
|
||||
});
|
||||
if (!user) {
|
||||
throw new Error('No such user found');
|
||||
}
|
||||
|
||||
const valid = await bcrypt.compare(
|
||||
args.password,
|
||||
user.password
|
||||
);
|
||||
if (!valid) {
|
||||
throw new Error('Invalid password');
|
||||
}
|
||||
|
||||
const token = jwt.sign({ userId: user.id }, APP_SECRET);
|
||||
|
||||
return {
|
||||
token,
|
||||
user
|
||||
};
|
||||
}
|
||||
|
||||
async function follow(parent, args, context, info) {
|
||||
const { userId } = context;
|
||||
const follow = await context.mongo.follow.findUnique({
|
||||
where: {
|
||||
linkId_userId: {
|
||||
linkId: Number(args.linkId),
|
||||
userId: userId
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (Boolean(follow)) {
|
||||
throw new Error(
|
||||
`Already followed the appointment: ${args.linkId}`
|
||||
);
|
||||
}
|
||||
|
||||
const newFollow = context.mongo.follow.create({
|
||||
data: {
|
||||
user: { connect: { id: userId } },
|
||||
link: { connect: { id: Number(args.linkId) } }
|
||||
}
|
||||
});
|
||||
context.pubsub.publish('NEW_FOLLOW', newFollow);
|
||||
|
||||
return newFollow;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createAppointment,
|
||||
signup,
|
||||
login,
|
||||
follow
|
||||
};
|
||||
30
server/src/resolvers/Query.js
Normal file
30
server/src/resolvers/Query.js
Normal file
@@ -0,0 +1,30 @@
|
||||
async function feed(parent, args, context, info) {
|
||||
console.log(context);
|
||||
const where = args.filter
|
||||
? {
|
||||
OR: [
|
||||
{ title: { contains: args.filter } },
|
||||
{ description: { contains: args.filter } }
|
||||
]
|
||||
}
|
||||
: {};
|
||||
|
||||
const appointments = await context.mongo.appointment.findMany({
|
||||
where,
|
||||
skip: args.skip,
|
||||
take: args.take,
|
||||
orderBy: args.orderBy
|
||||
});
|
||||
|
||||
const count = await context.mongo.appointment.count({ where });
|
||||
|
||||
return {
|
||||
id: 'main-feed',
|
||||
appointments,
|
||||
count
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
feed
|
||||
};
|
||||
26
server/src/resolvers/Subscription.js
Normal file
26
server/src/resolvers/Subscription.js
Normal file
@@ -0,0 +1,26 @@
|
||||
function newLinkSubscribe(parent, args, context, info) {
|
||||
return context.pubsub.asyncIterator("NEW_LINK")
|
||||
}
|
||||
|
||||
const newAppointment = {
|
||||
subscribe: newLinkSubscribe,
|
||||
resolve: payload => {
|
||||
return payload
|
||||
},
|
||||
}
|
||||
|
||||
function newFollowSubscribe(parent, args, context, info) {
|
||||
return context.pubsub.asyncIterator("NEW_FOLLOW")
|
||||
}
|
||||
|
||||
const newFollow = {
|
||||
subscribe: newFollowSubscribe,
|
||||
resolve: payload => {
|
||||
return payload
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
newAppointment,
|
||||
newFollow
|
||||
}
|
||||
9
server/src/resolvers/User.js
Normal file
9
server/src/resolvers/User.js
Normal file
@@ -0,0 +1,9 @@
|
||||
function appointments(parent, args, context) {
|
||||
return context.mongo.user
|
||||
.findUnique({ where: { id: parent.id } })
|
||||
.appointments();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
appointments
|
||||
};
|
||||
Reference in New Issue
Block a user