This commit is contained in:
Riccardo
2021-08-29 10:39:39 +02:00
parent 2f119e43bd
commit 5368f40b04
8 changed files with 75 additions and 131 deletions

View File

@@ -10,7 +10,6 @@ import fs from 'fs';
import path from 'path';
import cors from 'cors';
import jwt from 'jsonwebtoken';
const { APP_SECRET } = require('../utils');
const moduleURL = new URL(import.meta.url);
const __dirname = path.dirname(moduleURL.pathname);
@@ -20,7 +19,7 @@ const pubsub = new PubSub();
dotenv.config();
function getTokenPayload(token) {
return jwt.verify(token, APP_SECRET);
return jwt.verify(token, process.env.APP_SECRET);
}
function getUserId(req, authToken) {
@@ -58,17 +57,7 @@ const server = new ApolloServer({
// schema,
cors: true,
playground: process.env.NODE_ENV === 'development' ? true : false,
context: ({ req }) => {
// if (!db) {
// try {
// if (!dbClient.isConnected()) await dbClient.connect()
// mongo = dbClient.db('Calendar') // database name
// console.log(db);
// } catch (e) {
// console.log('--->error while connecting with graphql context (db)', e)
// }
return {
context: ({ req }) => ({
...req,
mongoose,
pubsub,
@@ -76,25 +65,24 @@ const server = new ApolloServer({
req && req.headers.authorization
? getUserId(req)
: null
};
}),
subscriptions: {
onConnect: (connectionParams) => {
if (connectionParams.authToken) {
return {
mongoose,
userId: getUserId(
null,
connectionParams.authToken
)
};
} else {
return {
mongoose
};
}
}
},
// subscriptions: {
// onConnect: (connectionParams) => {
// if (connectionParams.authToken) {
// return {
// mongoose,
// userId: getUserId(
// null,
// connectionParams.authToken
// )
// };
// } else {
// return {
// mongoose
// };
// }
// }
// },
introspection: true,
tracing: true,
path: '/',
@@ -118,36 +106,3 @@ server.applyMiddleware({
app.listen({ port: process.env.PORT }, () => {
console.log(`Server listening on port ${process.env.PORT}`);
});
// const { graphqlHTTP } = require('express-graphql');
// const mongoose = require("mongoose");
// const graphqlSchema = require("./graphql/schema/schema")
// const appointmentResolvers = require("./graphql/resolvers/appointment")
// const userResolvers = require("./graphql/resolvers/user")
// var MongoClient = require('mongodb', { useUnifiedTopology: true }).MongoClient;
// // import { MongoClient } from 'mongodb'
// const Query = require('./resolvers/Query');
// const Mutation = require('./resolvers/Mutation');
// const Subscription = require('./resolvers/Subscription');
// const User = require('./resolvers/User');
// const Appointment = require('./resolvers/Appointment');
// const Follow = require('./resolvers/Follow');
// const fs = require('fs');
// const path = require('path');
// const { getUserId } = require('./utils');
// const graphqlResolvers = {
// appointmentResolvers,
// userResolvers
// };
// // const resolvers = {
// // Query,
// // Mutation,
// // Subscription,
// // User,
// // Appointment,
// // Follow
// // };

View File

@@ -1,6 +1,7 @@
import Appointment from './models/appointment.js';
import User from './models/user.js';
import jwt from 'jsonwebtoken';
import { createAppointment } from './resolvers/Mutation';
export const resolvers = {
Query: {
@@ -30,11 +31,7 @@ export const resolvers = {
};
},
async login(parent, args, context) {
console.log(context);
const { userId } = context;
console.log(userId);
async login(parent, args) {
const user = await User.findOne({
email: args.email
});
@@ -55,15 +52,9 @@ export const resolvers = {
},
async createAppointment(parent, args, context) {
console.log(context);
const { userId } = context;
console.log("userID", userId);
args.deleted = false;
args.createdBy = userId;
return await Appointment.create(args);
return await createAppointment(parent, args, context);
},
async updateAppointment(parent, args) {
console.log(args);
return await Appointment.findOneAndUpdate({
args
}, args, {

View File

@@ -1,19 +1,16 @@
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const { APP_SECRET } = require('../utils');
import dotenv from 'dotenv';
import appointment from '../models/appointment';
function createAppointment(parent, args, context) {
dotenv.config();
async function createAppointment(parent, args, context) {
const { userId } = context;
const newAppointment = context.mongo.appointment.create({
data: {
title: args.title,
description: args.description,
createdBy: { connect: { id: userId } }
}
});
return newAppointment;
args.deleted = false;
args.createdBy = userId;
console.log(parent, args, context);
return await appointment.create(args);
}
async function signup(parent, args, context) {
@@ -22,7 +19,7 @@ async function signup(parent, args, context) {
data: { ...args, password }
});
const token = jwt.sign({ userId: user.id }, APP_SECRET);
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
return {
token,
@@ -46,7 +43,7 @@ async function login(parent, args, context) {
throw new Error('Invalid password');
}
const token = jwt.sign({ userId: user.id }, APP_SECRET);
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
return {
token,

View File

@@ -59,7 +59,7 @@ input UserInput {
username: String!
email: String!
password: String!
appointments: [Appointment!]!
appointments: [AppointmentInput!]!
}
type AuthPayload {
token: String
@@ -75,7 +75,7 @@ type Appointment {
start: DateTime!
end: DateTime!
deleted: Boolean
user: User
createdBy: User
# follows: [Follow!]!
}
input AppointmentInput {

View File

@@ -1,8 +1,10 @@
import jwt from 'jsonwebtoken';
const APP_SECRET = 'GraphQL-is-aw3some';
import dotenv from 'dotenv';
dotenv.config();
function getTokenPayload(token) {
return jwt.verify(token, APP_SECRET);
return jwt.verify(token, process.env.APP_SECRET);
}
function getUserId(req, authToken) {
@@ -25,6 +27,5 @@ function getUserId(req, authToken) {
}
module.exports = {
APP_SECRET,
getUserId
};