From 5368f40b04df6d705d43e78585595a817ec2670c Mon Sep 17 00:00:00 2001 From: Riccardo Date: Sun, 29 Aug 2021 10:39:39 +0200 Subject: [PATCH] Progress --- .../src/components/appointment/Appointment.js | 8 +- .../appointment/CreateAppointment.js | 52 ++++++------ client/src/components/layout/Header.js | 12 +-- server/src/index.js | 83 +++++-------------- server/src/resolvers.js | 15 +--- server/src/resolvers/Mutation.js | 25 +++--- server/src/schema.graphql | 4 +- server/src/utils.js | 7 +- 8 files changed, 75 insertions(+), 131 deletions(-) diff --git a/client/src/components/appointment/Appointment.js b/client/src/components/appointment/Appointment.js index fd5fbc6..5efd5c4 100644 --- a/client/src/components/appointment/Appointment.js +++ b/client/src/components/appointment/Appointment.js @@ -21,10 +21,10 @@ const Appointment = (props) => { onCompleted: () => history.push('/') }) - const updateAppointment = () => { - let path = `/update/${appointment._id}`; - history.push(path); - } + // const updateAppointment = () => { + // let path = `/update/${appointment._id}`; + // history.push(path); + // } return (
diff --git a/client/src/components/appointment/CreateAppointment.js b/client/src/components/appointment/CreateAppointment.js index 2a348a6..1fe83ae 100644 --- a/client/src/components/appointment/CreateAppointment.js +++ b/client/src/components/appointment/CreateAppointment.js @@ -43,34 +43,34 @@ const CreateAppointment = () => { start: formState.start, end: formState.end }, - // update: (cache, { data: { createAppointment } }) => { - // const take = APPOINTMENTS_PER_PAGE; - // const skip = 0; - // const orderBy = { createdAt: 'desc' }; + update: (cache, { data: { createAppointment } }) => { + const take = APPOINTMENTS_PER_PAGE; + const skip = 0; + const orderBy = { createdAt: 'desc' }; - // const data = cache.readQuery({ - // query: APPOINTMENTS_QUERY, - // variables: { - // take, - // skip, - // orderBy - // } - // }); + const data = cache.readQuery({ + query: APPOINTMENTS_QUERY, + variables: { + take, + skip, + orderBy + } + }); - // cache.writeQuery({ - // query: APPOINTMENTS_QUERY, - // data: { - // allAppointments: { - // appointments: [createAppointment, ...data.allAppointments] - // } - // }, - // variables: { - // take, - // skip, - // orderBy - // } - // }); - // }, + cache.writeQuery({ + query: APPOINTMENTS_QUERY, + data: { + allAppointments: { + appointments: [createAppointment, ...data.allAppointments] + } + }, + variables: { + take, + skip, + orderBy + } + }); + }, onCompleted: () => history.push('/') }); diff --git a/client/src/components/layout/Header.js b/client/src/components/layout/Header.js index 33e38aa..5c86bf4 100644 --- a/client/src/components/layout/Header.js +++ b/client/src/components/layout/Header.js @@ -1,11 +1,11 @@ import React from 'react'; -// import { useHistory } from 'react-router'; +import { useHistory } from 'react-router'; import { Link } from 'react-router-dom'; -// import { AUTH_TOKEN } from '../../constants'; +import { AUTH_TOKEN } from '../../constants'; const Header = () => { - // const history = useHistory(); - // const authToken = localStorage.getItem(AUTH_TOKEN); + const history = useHistory(); + const authToken = localStorage.getItem(AUTH_TOKEN); return (
@@ -20,7 +20,7 @@ const Header = () => { {/*
Search
*/} - {/*
+
{authToken ? (
{ @@ -31,7 +31,7 @@ const Header = () => { ) : ( Login )} -
*/} +
); }; diff --git a/server/src/index.js b/server/src/index.js index 399bfa9..ee363ac 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -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 -// // }; diff --git a/server/src/resolvers.js b/server/src/resolvers.js index 9691a39..337daf0 100644 --- a/server/src/resolvers.js +++ b/server/src/resolvers.js @@ -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, { diff --git a/server/src/resolvers/Mutation.js b/server/src/resolvers/Mutation.js index 0fc9393..4e68b00 100644 --- a/server/src/resolvers/Mutation.js +++ b/server/src/resolvers/Mutation.js @@ -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, diff --git a/server/src/schema.graphql b/server/src/schema.graphql index 50939c6..5f02b00 100644 --- a/server/src/schema.graphql +++ b/server/src/schema.graphql @@ -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 { diff --git a/server/src/utils.js b/server/src/utils.js index f8cbbc0..87d943d 100644 --- a/server/src/utils.js +++ b/server/src/utils.js @@ -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 };