Started with Mongo

This commit is contained in:
Riccardo
2021-01-02 13:11:59 +01:00
parent 5294122f61
commit 30407cac0a
41 changed files with 33775 additions and 0 deletions

View 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
};

View 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
};

View 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
};

View 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
};

View 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
}

View File

@@ -0,0 +1,9 @@
function appointments(parent, args, context) {
return context.mongo.user
.findUnique({ where: { id: parent.id } })
.appointments();
}
module.exports = {
appointments
};