From 9794f5c796f7e29eccaf4680c4f1c420b8c27577 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Wed, 6 Jan 2021 12:15:53 +0100 Subject: [PATCH] Delete Appointment works --- client/src/components/Appointment.js | 21 +++++++++- client/src/components/AppointmentList.js | 4 +- server/package.json | 1 + server/src/index.js | 2 + server/src/resolvers.js | 51 +++++------------------- server/src/schema.graphql | 5 ++- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/client/src/components/Appointment.js b/client/src/components/Appointment.js index 17078bd..8ecb357 100644 --- a/client/src/components/Appointment.js +++ b/client/src/components/Appointment.js @@ -1,12 +1,29 @@ import React from 'react'; +import { useMutation, gql } from '@apollo/client'; + +const DELETE_APPOINTMENT_MUTATION = gql` + mutation DeleteAppointmentMutation($_id: ID!) { + deleteAppointment(_id: $_id){ + _id + } + } +`; + const Appointment = (props) => { const { appointment } = props; + + const [deleteAppointment] = useMutation(DELETE_APPOINTMENT_MUTATION, { + variables: { _id: appointment._id } + }) + + console.log(appointment._id); + return (
- {appointment.title} starts at {appointment.start}, ends at {appointment.end}. It is described as "{appointment.description}" -
+
[X]
{appointment.title}
starts at {appointment.start}, ends at {appointment.end}. It is described as "{appointment.description}" +
); }; diff --git a/client/src/components/AppointmentList.js b/client/src/components/AppointmentList.js index 52c863b..14c5394 100644 --- a/client/src/components/AppointmentList.js +++ b/client/src/components/AppointmentList.js @@ -5,6 +5,7 @@ import { useQuery, gql } from '@apollo/client'; export const APPOINTMENTS_QUERY = gql` { allAppointments{ + _id title description start @@ -18,12 +19,11 @@ const AppointmentList = () => { const { data } = useQuery(APPOINTMENTS_QUERY); if (data !== undefined) { - console.log(data.allAppointments); return (
{ data.allAppointments.map((appointment) => ( - + )) }
diff --git a/server/package.json b/server/package.json index abe8a1c..6b2ada6 100644 --- a/server/package.json +++ b/server/package.json @@ -27,6 +27,7 @@ "graphql-compose": "^7.23.0", "graphql-compose-connection": "^8.0.1", "graphql-compose-mongoose": "^9.0.0", + "graphql-depth-limit": "^1.1.0", "graphql-middleware": "^6.0.0", "graphql-tools": "^7.0.2", "jsonwebtoken": "8.5.1", diff --git a/server/src/index.js b/server/src/index.js index 0b90e19..5c82b2b 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -1,6 +1,7 @@ import express from 'express'; import dotenv from 'dotenv'; import { graphqlHTTP } from 'express-graphql'; +import depthLimit from 'graphql-depth-limit' import { ApolloServer, PubSub } from 'apollo-server-express'; import mongoose from 'mongoose'; import schema from './schema.js'; @@ -26,6 +27,7 @@ app.use(cors()); app.use('/djhb58fytkh476dk45yh49', graphqlHTTP({ schema: schema, + validationRules: [depthLimit(3)], graphiql: true })); diff --git a/server/src/resolvers.js b/server/src/resolvers.js index 759fa18..1d389ed 100644 --- a/server/src/resolvers.js +++ b/server/src/resolvers.js @@ -66,55 +66,22 @@ export const resolvers = { }; }, - // async login(parent, args, context, info) { - // console.log(args); - // const user = await User.findOne({ - // email: args.email - // }); - - // if (!user) { - // throw new Error('No such user found'); - // } - - // const pwd = await bcrypt.hash(args.password, 10); - // console.log(pwd); - // console.log(args.password) - // console.log(user.password); - - // const valid = await bcrypt.compare( - // args.password, - // user.password - // ); - // if (!valid) { - // throw new Error('Invalid password'); - // } - - // const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET); - - // return { - // token, - // user - // }; - // }, - async createAppointment(parent, args, context, info) { return await Appointment.create(args); }, - async updateAppointment(root, { - _id, - input - }) { + async updateAppointment(parent, args, context, info) { return await Appointment.findOneAndUpdate({ - _id - }, input, { + args + }, args, { new: true }) }, - async deleteAppointment(root, { - _id - }) { - return await Product.findOneAndRemove({ - _id + async deleteAppointment(parent, args, context, info) { + console.log(args); + return await Appointment.deleteOne({ _id: args._id }).then(function () { + console.log("Data deleted"); // Success + }).catch(function (error) { + console.log(error); // Failure }); }, async createProduct(root, { diff --git a/server/src/schema.graphql b/server/src/schema.graphql index 239aff8..fc420a0 100644 --- a/server/src/schema.graphql +++ b/server/src/schema.graphql @@ -24,11 +24,12 @@ type Mutation { description: String! start: String! end: String! - # input: AppointmentInput ): Appointment! updateAppointment( _id: ID!, - input: AppointmentInput + title: String! + description: String! + start: String! ): Appointment deleteAppointment( _id: ID!