Appointment with Mongo
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import AppointmentList from './AppointmentList';
|
import AppointmentList from './AppointmentList';
|
||||||
import CreateLink from './CreateAppointment'
|
import CreateAppointment from './CreateAppointment'
|
||||||
import Header from './Header';
|
import Header from './Header';
|
||||||
import Login from './Login'
|
import Login from './Login'
|
||||||
import Search from './Search';
|
import Search from './Search';
|
||||||
@@ -19,7 +19,7 @@ const App = () => {
|
|||||||
<Route
|
<Route
|
||||||
exact
|
exact
|
||||||
path="/create"
|
path="/create"
|
||||||
component={CreateLink}
|
component={CreateAppointment}
|
||||||
/>
|
/>
|
||||||
<Route exact path="/login" component={Login} />
|
<Route exact path="/login" component={Login} />
|
||||||
<Route exact path="/search" component={Search} />
|
<Route exact path="/search" component={Search} />
|
||||||
@@ -34,31 +34,4 @@ const App = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// class App extends Component {
|
|
||||||
// render() {
|
|
||||||
// return <CreateLink />;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// function App() {
|
|
||||||
// return (
|
|
||||||
// <div className="App">
|
|
||||||
// <header className="App-header">
|
|
||||||
// <img src={logo} className="App-logo" alt="logo" />
|
|
||||||
// <p>
|
|
||||||
// Edit <code>src/App.js</code> and save to reload.
|
|
||||||
// </p>
|
|
||||||
// <a
|
|
||||||
// className="App-link"
|
|
||||||
// href="https://reactjs.org"
|
|
||||||
// target="_blank"
|
|
||||||
// rel="noopener noreferrer"
|
|
||||||
// >
|
|
||||||
// Learn React
|
|
||||||
// </a>
|
|
||||||
// </header>
|
|
||||||
// </div>
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ const getQueryVariables = (isNewPage, page) => {
|
|||||||
const skip = isNewPage ? (page - 1) * APPOINTMENTS_PER_PAGE : 0;
|
const skip = isNewPage ? (page - 1) * APPOINTMENTS_PER_PAGE : 0;
|
||||||
const take = isNewPage ? APPOINTMENTS_PER_PAGE : 100;
|
const take = isNewPage ? APPOINTMENTS_PER_PAGE : 100;
|
||||||
const orderBy = { createdAt: 'desc' };
|
const orderBy = { createdAt: 'desc' };
|
||||||
console.log(isNewPage, page, APPOINTMENTS_PER_PAGE, skip, take, orderBy);
|
|
||||||
return { take, skip, orderBy };
|
return { take, skip, orderBy };
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -78,8 +78,6 @@ const AppointmentList = () => {
|
|||||||
pageIndexParams[pageIndexParams.length - 1]
|
pageIndexParams[pageIndexParams.length - 1]
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(pageIndexParams.length, page);
|
|
||||||
|
|
||||||
const pageIndex = page ? (page - 1) * APPOINTMENTS_PER_PAGE : 0;
|
const pageIndex = page ? (page - 1) * APPOINTMENTS_PER_PAGE : 0;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ import { FEED_QUERY } from './AppointmentList';
|
|||||||
|
|
||||||
const CREATE_APPOINTMENT_MUTATION = gql`
|
const CREATE_APPOINTMENT_MUTATION = gql`
|
||||||
mutation CreateAppointmentMutation(
|
mutation CreateAppointmentMutation(
|
||||||
|
$title: String!
|
||||||
$description: String!
|
$description: String!
|
||||||
$url: String!
|
|
||||||
) {
|
) {
|
||||||
createAppointment(description: $description, url: $url) {
|
createAppointment(title: $title, description: $description) {
|
||||||
id
|
id
|
||||||
createdAt
|
title
|
||||||
url
|
|
||||||
description
|
description
|
||||||
|
createdAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|||||||
31
server/src/graphql/models/appointment.js
Normal file
31
server/src/graphql/models/appointment.js
Normal 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)
|
||||||
36
server/src/graphql/models/user.js
Normal file
36
server/src/graphql/models/user.js
Normal 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)
|
||||||
36
server/src/graphql/resolvers/appointment.js
Normal file
36
server/src/graphql/resolvers/appointment.js
Normal 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
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
36
server/src/graphql/resolvers/user.js
Normal file
36
server/src/graphql/resolvers/user.js
Normal 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
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
53
server/src/graphql/schema/schema.js
Normal file
53
server/src/graphql/schema/schema.js
Normal 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
|
||||||
|
`)
|
||||||
@@ -1,4 +1,14 @@
|
|||||||
const { ApolloServer, PubSub } = require('apollo-server');
|
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;
|
var MongoClient = require('mongodb', { useUnifiedTopology: true }).MongoClient;
|
||||||
// import { MongoClient } from 'mongodb'
|
// import { MongoClient } from 'mongodb'
|
||||||
const Query = require('./resolvers/Query');
|
const Query = require('./resolvers/Query');
|
||||||
@@ -13,87 +23,126 @@ const { getUserId } = require('./utils');
|
|||||||
|
|
||||||
const pubsub = new PubSub();
|
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({
|
// const mongo = new MongoClient({
|
||||||
// errorFormat: 'minimal'
|
// 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;
|
// if (err) throw err;
|
||||||
console.log("ALL good");
|
// console.log("ALL good");
|
||||||
//Write databse Insert/Update/Query code here..
|
// //Write databse Insert/Update/Query code here..
|
||||||
|
|
||||||
});
|
// });
|
||||||
|
|
||||||
const resolvers = {
|
// const dbClient = new MongoClient(
|
||||||
Query,
|
// 'mongodb+srv://admin:hEbAjhvkrFDHAP3@cluster0.0hjtt.mongodb.net/Calendar?retryWrites=true&w=majority',
|
||||||
Mutation,
|
// {
|
||||||
Subscription,
|
// useNewUrlParser: true,
|
||||||
User,
|
// useUnifiedTopology: true,
|
||||||
Appointment,
|
// }
|
||||||
Follow
|
// )
|
||||||
};
|
|
||||||
|
|
||||||
let db;
|
// const resolvers = {
|
||||||
|
// Query,
|
||||||
|
// Mutation,
|
||||||
|
// Subscription,
|
||||||
|
// User,
|
||||||
|
// Appointment,
|
||||||
|
// Follow
|
||||||
|
// };
|
||||||
|
|
||||||
const server = new ApolloServer({
|
// let db;
|
||||||
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,
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
|
|
||||||
// if (!dbClient.isConnected()) await dbClient.connect()
|
// const server = new ApolloServer({
|
||||||
// db = dbClient.db('next-graphql') // database name
|
// typeDefs: fs.readFileSync(
|
||||||
// } catch (e) {
|
// path.join(__dirname, 'schema.graphql'),
|
||||||
// console.log('--->error while connecting with graphql context (db)', e)
|
// '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 }
|
// return {
|
||||||
// },
|
// ...req,
|
||||||
context: ({ req }) => {
|
// mongo,
|
||||||
return {
|
// pubsub,
|
||||||
...req,
|
// userId:
|
||||||
mongo,
|
// req && req.headers.authorization
|
||||||
pubsub,
|
// ? getUserId(req)
|
||||||
userId:
|
// : null
|
||||||
req && req.headers.authorization
|
// }
|
||||||
? getUserId(req)
|
// },
|
||||||
: null
|
// // context: ({ req }) => {
|
||||||
};
|
// // return {
|
||||||
},
|
// // ...req,
|
||||||
subscriptions: {
|
// // mongo,
|
||||||
onConnect: (connectionParams) => {
|
// // pubsub,
|
||||||
if (connectionParams.authToken) {
|
// // userId:
|
||||||
return {
|
// // req && req.headers.authorization
|
||||||
mongo,
|
// // ? getUserId(req)
|
||||||
userId: getUserId(
|
// // : null
|
||||||
null,
|
// // };
|
||||||
connectionParams.authToken
|
// // },
|
||||||
)
|
// // subscriptions: {
|
||||||
};
|
// // onConnect: (connectionParams) => {
|
||||||
} else {
|
// // if (connectionParams.authToken) {
|
||||||
return {
|
// // return {
|
||||||
mongo
|
// // mongo,
|
||||||
};
|
// // userId: getUserId(
|
||||||
}
|
// // null,
|
||||||
}
|
// // connectionParams.authToken
|
||||||
}
|
// // )
|
||||||
});
|
// // };
|
||||||
|
// // } else {
|
||||||
|
// // return {
|
||||||
|
// // mongo
|
||||||
|
// // };
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
// });
|
||||||
|
|
||||||
server
|
// // server.applyMiddleware({ app });
|
||||||
.listen()
|
|
||||||
.then(({ url }) =>
|
// server
|
||||||
console.log(`Server is running on ${url}`)
|
// .listen()
|
||||||
);
|
// .then(({ url }) =>
|
||||||
|
// console.log(`Server is running on ${url}`)
|
||||||
|
// );
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ function createAppointment(parent, args, context, info) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function signup(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 password = await bcrypt.hash(args.password, 10);
|
||||||
const user = await context.mongo.user.create({
|
const user = await context.mongo.user.create({
|
||||||
data: { ...args, password }
|
data: { ...args, password }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
async function feed(parent, args, context, info) {
|
async function feed(parent, args, context, info) {
|
||||||
console.log(context);
|
|
||||||
const where = args.filter
|
const where = args.filter
|
||||||
? {
|
? {
|
||||||
OR: [
|
OR: [
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ type Query {
|
|||||||
take: Int
|
take: Int
|
||||||
orderBy: AppointmentOrderByInput
|
orderBy: AppointmentOrderByInput
|
||||||
): Feed!
|
): Feed!
|
||||||
|
# users: [User!]!
|
||||||
}
|
}
|
||||||
|
|
||||||
type Feed {
|
type Feed {
|
||||||
@@ -26,8 +27,13 @@ type Mutation {
|
|||||||
password: String!
|
password: String!
|
||||||
name: String!
|
name: String!
|
||||||
): AuthPayload
|
): AuthPayload
|
||||||
login(email: String!, password: String!): AuthPayload
|
login(
|
||||||
follow(appointmentId: ID!): Follow
|
email: String!,
|
||||||
|
password: String!
|
||||||
|
): AuthPayload
|
||||||
|
follow(
|
||||||
|
appointmentId: ID!
|
||||||
|
): Follow
|
||||||
}
|
}
|
||||||
|
|
||||||
type Subscription {
|
type Subscription {
|
||||||
|
|||||||
Reference in New Issue
Block a user