Appointment with Mongo

This commit is contained in:
Riccardo
2021-01-02 18:37:03 +01:00
parent 30407cac0a
commit c752e3ec80
12 changed files with 330 additions and 112 deletions

View File

@@ -0,0 +1,31 @@
const mongoose = require("mongoose")
const Schema = mongoose.Schema
const appointmentSchema = new Schema(
{
title: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
timeStart: {
type: Date,
required: true,
},
timeEnd: {
type: Date,
required: true,
},
},
{ timestamps: true }
)
module.exports = mongoose.model("Appointment", appointmentSchema)

View File

@@ -0,0 +1,36 @@
const mongoose = require("mongoose")
const Schema = mongoose.Schema
const userSchema = new Schema(
{
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
isActive: {
type: Boolean,
required: true,
default: 1
},
},
{ timestamps: true }
)
module.exports = mongoose.model("User", userSchema)

View File

@@ -0,0 +1,36 @@
// const { default: Appointment } = require("../../../../client/src/components/Appointment")
const { newAppointment } = require("../../resolvers/Subscription")
const Appointment = require("../models/appointment")
module.exports = {
findAppointments: async () => {
try {
const appointmentsFetched = await Appointment.find()
return appointmentsFetched.map(appointment => {
return {
...this.appointment._doc,
_id: appointment.id,
createdAt: new Date(appointment._doc.createdAt).toISOString(),
}
})
} catch (error) {
throw error
}
},
createAppointment: async args => {
try {
const { title, description, timeStart, timeEnd } = args.appointment
const appointment = new Appointment({
title,
description,
timeStart,
timeEnd
})
const newAppointment = await appointment.save()
return { ...newAppointment._doc, _id: newAppointment.id }
} catch (error) {
throw error
}
},
}

View File

@@ -0,0 +1,36 @@
// const { default: Appointment } = require("../../../../client/src/components/Appointment")
// const { newUser } = require("../../resolvers/Subscription")
const User = require("../models/user")
module.exports = {
findUsers: async () => {
try {
const usersFetched = await User.find()
return usersFetched.map(user => {
return {
...this.user._doc,
_id: user.id,
createdAt: new Date(user._doc.createdAt).toISOString(),
}
})
} catch (error) {
throw error
}
},
createUser: async args => {
try {
const { firstName, lastName, email, isActive } = args.user
const user = new User({
firstName,
lastName,
email,
isActive
})
const newUser = await user.save()
return { ...newUser._doc, _id: newUser.id }
} catch (error) {
throw error
}
},
}

View File

@@ -0,0 +1,53 @@
const { buildSchema } = require("graphql")
module.exports = buildSchema(`
type User {
_id: ID!
firstName: String!
lastName: String!
email: String!
password: String!
isActive: Boolean!
}
input UserInput {
firstName: String!
lastName: String!
email: String!
password: String!
isActive: Boolean!
}
type Appointment {
_id: ID!
title: String!
description: String!
timeStart: Date!
timeEnd: Date!
createdAt: String!
}
input AppointmentInput {
title: String!
description: String!
timeStart: Date!
timeEnd: Date!
}
type Query {
findAppointments:[Appointment!]
findUsers:[User!]!
}
type Mutation {
createAppointment(appointment:AppointmentInput): Appointment
createUser(user:UserInput): User
}
schema {
query: Query
mutation: Mutation
}
scalar Date
`)

View File

@@ -1,4 +1,14 @@
const { ApolloServer, PubSub } = require('apollo-server');
// const { Cors } = require('cors');
// const { Express } = require('express');
const express = require("express");
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');
@@ -13,87 +23,126 @@ const { getUserId } = require('./utils');
const pubsub = new PubSub();
const app = express()
const graphqlResolvers = {
appointmentResolvers,
userResolvers
};
app.use(
"/graphql",
graphqlHTTP({
schema: graphqlSchema,
rootValue: appointmentResolvers,
graphiql: true,
})
)
const uri = `mongodb+srv://admin:hEbAjhvkrFDHAP3@cluster0.0hjtt.mongodb.net/Calendar?retryWrites=true&w=majority`
const options = { useNewUrlParser: true, useUnifiedTopology: true }
let db = mongoose
.connect(uri, options)
.then(() => app.listen(4000, console.log("Server is running")))
.catch(error => {
throw error
})
// const app = new Express();
// app.use(Cors());
// const mongo = new MongoClient({
// errorFormat: 'minimal'
// });
const mongo = MongoClient.connect("mongodb+srv://admin:hEbAjhvkrFDHAP3@cluster0.0hjtt.mongodb.net/Calendar?retryWrites=true&w=majority", function (err, db) {
// const mongo = MongoClient.connect("mongodb+srv://admin:hEbAjhvkrFDHAP3@cluster0.0hjtt.mongodb.net/Calendar?retryWrites=true&w=majority", function (err, db) {
if (err) throw err;
console.log("ALL good");
//Write databse Insert/Update/Query code here..
// if (err) throw err;
// console.log("ALL good");
// //Write databse Insert/Update/Query code here..
});
// });
const resolvers = {
Query,
Mutation,
Subscription,
User,
Appointment,
Follow
};
// const dbClient = new MongoClient(
// 'mongodb+srv://admin:hEbAjhvkrFDHAP3@cluster0.0hjtt.mongodb.net/Calendar?retryWrites=true&w=majority',
// {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// }
// )
let db;
// const resolvers = {
// Query,
// Mutation,
// Subscription,
// User,
// Appointment,
// Follow
// };
const server = new ApolloServer({
typeDefs: fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8'
),
resolvers,
// context: async () => {
// if (!db) {
// try {
// const dbClient = new MongoClient(
// 'mongodb+srv://test:qwerty123@cluster0-yvwjx.mongodb.net/next-graphql?retryWrites=true&w=majority',
// {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// }
// )
// let db;
// if (!dbClient.isConnected()) await dbClient.connect()
// db = dbClient.db('next-graphql') // database name
// } catch (e) {
// console.log('--->error while connecting with graphql context (db)', e)
// }
// }
// const server = new ApolloServer({
// typeDefs: fs.readFileSync(
// path.join(__dirname, 'schema.graphql'),
// 'utf8'
// ),
// resolvers,
// context: async ({ 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 { db }
// },
context: ({ req }) => {
return {
...req,
mongo,
pubsub,
userId:
req && req.headers.authorization
? getUserId(req)
: null
};
},
subscriptions: {
onConnect: (connectionParams) => {
if (connectionParams.authToken) {
return {
mongo,
userId: getUserId(
null,
connectionParams.authToken
)
};
} else {
return {
mongo
};
}
}
}
});
// return {
// ...req,
// mongo,
// pubsub,
// userId:
// req && req.headers.authorization
// ? getUserId(req)
// : null
// }
// },
// // context: ({ req }) => {
// // return {
// // ...req,
// // mongo,
// // pubsub,
// // userId:
// // req && req.headers.authorization
// // ? getUserId(req)
// // : null
// // };
// // },
// // subscriptions: {
// // onConnect: (connectionParams) => {
// // if (connectionParams.authToken) {
// // return {
// // mongo,
// // userId: getUserId(
// // null,
// // connectionParams.authToken
// // )
// // };
// // } else {
// // return {
// // mongo
// // };
// // }
// // }
// // }
// });
server
.listen()
.then(({ url }) =>
console.log(`Server is running on ${url}`)
);
// // server.applyMiddleware({ app });
// server
// .listen()
// .then(({ url }) =>
// console.log(`Server is running on ${url}`)
// );

View File

@@ -17,7 +17,7 @@ function createAppointment(parent, args, context, info) {
}
async function signup(parent, args, context, info) {
console.log(context);
console.log(context.mongo);
const password = await bcrypt.hash(args.password, 10);
const user = await context.mongo.user.create({
data: { ...args, password }

View File

@@ -1,5 +1,5 @@
async function feed(parent, args, context, info) {
console.log(context);
const where = args.filter
? {
OR: [

View File

@@ -6,6 +6,7 @@ type Query {
take: Int
orderBy: AppointmentOrderByInput
): Feed!
# users: [User!]!
}
type Feed {
@@ -26,8 +27,13 @@ type Mutation {
password: String!
name: String!
): AuthPayload
login(email: String!, password: String!): AuthPayload
follow(appointmentId: ID!): Follow
login(
email: String!,
password: String!
): AuthPayload
follow(
appointmentId: ID!
): Follow
}
type Subscription {