diff --git a/server/package.json b/server/package.json index 4c7e759..abe8a1c 100644 --- a/server/package.json +++ b/server/package.json @@ -39,9 +39,11 @@ "node-migrate": "^0.1.0" }, "devDependencies": { + "babel-cli": "^6.26.0", "babel-eslint": "^10.1.0", "babel-loader": "^8.2.2", "babel-preset-env": "^1.7.0", + "babel-preset-stage-0": "^6.24.1", "eslint": "^7.17.0", "eslint-plugin-babel": "^5.3.1", "eslint-plugin-import": "^2.22.1", diff --git a/server/src/graphql/models/appointment.js b/server/src/graphql/models/appointment.js deleted file mode 100644 index f6cf5c6..0000000 --- a/server/src/graphql/models/appointment.js +++ /dev/null @@ -1,31 +0,0 @@ -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) \ No newline at end of file diff --git a/server/src/graphql/models/user.js b/server/src/graphql/models/user.js deleted file mode 100644 index afc5b8c..0000000 --- a/server/src/graphql/models/user.js +++ /dev/null @@ -1,36 +0,0 @@ -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) \ No newline at end of file diff --git a/server/src/graphql/resolvers/appointment.js b/server/src/graphql/resolvers/appointment.js deleted file mode 100644 index eef5007..0000000 --- a/server/src/graphql/resolvers/appointment.js +++ /dev/null @@ -1,36 +0,0 @@ -// 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 - } - }, -} \ No newline at end of file diff --git a/server/src/graphql/resolvers/user.js b/server/src/graphql/resolvers/user.js deleted file mode 100644 index c318e86..0000000 --- a/server/src/graphql/resolvers/user.js +++ /dev/null @@ -1,36 +0,0 @@ -// 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 - } - }, -} \ No newline at end of file diff --git a/server/src/graphql/schema/schema.js b/server/src/graphql/schema/schema.js deleted file mode 100644 index 0701576..0000000 --- a/server/src/graphql/schema/schema.js +++ /dev/null @@ -1,53 +0,0 @@ -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 -`) \ No newline at end of file diff --git a/server/src/index.js b/server/src/index.js index 20b672b..75611d4 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -1,17 +1,37 @@ -import dotenv from 'dotenv'; import express from 'express'; +import dotenv from 'dotenv'; +import { graphqlHTTP } from 'express-graphql'; import { ApolloServer, PubSub } from 'apollo-server-express'; import mongoose from 'mongoose'; +import schema from './schema.js'; import './utils/db.js'; -import schema from './schema/index.js'; +import fs from 'fs'; +import path from 'path'; + +const moduleURL = new URL(import.meta.url); +const __dirname = path.dirname(moduleURL.pathname); +const app = express(); +const pubsub = new PubSub(); +const PORT = 4000; dotenv.config(); -const app = express(); -const pubsub = new PubSub(); +app.get('/', (req, res) => { + res.json({ + msg: 'Welcome to GraphQL' + }) +}); + +app.use('/graphql', graphqlHTTP({ + schema: schema, + graphiql: true +})); const server = new ApolloServer({ - schema, + typeDefs: fs.readFileSync( + path.join(__dirname, 'schema.graphql'), + 'utf8' + ), cors: true, playground: process.env.NODE_ENV === 'development' ? true : false, context: async ({ req }) => { @@ -27,30 +47,30 @@ const server = new ApolloServer({ return { ...req, mongoose, - pubsub, - userId: - req && req.headers.authorization - ? getUserId(req) - : null - } - }, - subscriptions: { - onConnect: (connectionParams) => { - if (connectionParams.authToken) { - return { - mongoose, - userId: getUserId( - null, - connectionParams.authToken - ) - }; - } else { - return { - mongoose - }; - } + // pubsub, + // userId: + // req && req.headers.authorization + // ? getUserId(req) + // : null } }, + // subscriptions: { + // onConnect: (connectionParams) => { + // if (connectionParams.authToken) { + // return { + // mongoose, + // userId: getUserId( + // null, + // connectionParams.authToken + // ) + // }; + // } else { + // return { + // mongoose + // }; + // } + // } + // }, introspection: true, tracing: true, path: '/', @@ -78,6 +98,9 @@ app.listen({ port: process.env.PORT }, () => { + + + // const { ApolloServer, PubSub } = require('apollo-server'); // // const { Cors } = require('cors'); // // const { Express } = require('express'); diff --git a/server/src/models/appointment.js b/server/src/models/appointment.js deleted file mode 100644 index 1fa9f18..0000000 --- a/server/src/models/appointment.js +++ /dev/null @@ -1,43 +0,0 @@ -import mongoose from 'mongoose'; -import timestamps from 'mongoose-timestamp'; -import { composeWithMongoose } from 'graphql-compose-mongoose'; - -export const AppointmentSchema = new mongoose.Schema( - { - user: { - type: mongoose.Schema.Types.ObjectId, - ref: 'User', - required: true, - }, - - title: { - type: String, - required: true, - }, - - description: { - type: String, - required: true, - }, - - timeStart: { - type: Date, - required: true, - }, - - timeEnd: { - type: Date, - required: true, - }, - }, - { - collection: 'appointment', - } -); - -AppointmentSchema.plugin(timestamps); - -AppointmentSchema.index({ createdAt: 1, updatedAt: 1 }); - -export const Appointment = mongoose.model('Appointment', AppointmentSchema); -export const AppointmentTC = composeWithMongoose(Appointment); \ No newline at end of file diff --git a/server/src/models/product.js b/server/src/models/product.js new file mode 100644 index 0000000..917b4b9 --- /dev/null +++ b/server/src/models/product.js @@ -0,0 +1,12 @@ +import mongoose from 'mongoose'; +const Schema = mongoose.Schema; +const ProductSchema = new Schema({ + title: { + type: String, + required: true + }, + qty: { + type: Number + } +}); +export default mongoose.model('product', ProductSchema); \ No newline at end of file diff --git a/server/src/models/user.js b/server/src/models/user.js deleted file mode 100644 index e91a4a4..0000000 --- a/server/src/models/user.js +++ /dev/null @@ -1,45 +0,0 @@ -import mongoose from 'mongoose'; -import timestamps from 'mongoose-timestamp'; -import { composeWithMongoose } from 'graphql-compose-mongoose'; - -export const UserSchema = new mongoose.Schema( - { - firstName: { - type: String, - required: true, - }, - - lastName: { - type: String, - required: true, - }, - - password: { - type: String, - required: true, - }, - - email: { - type: String, - lowercase: true, - unique: true, - required: true, - }, - - isActive: { - type: Boolean, - required: true, - default: 1 - }, - }, - { - collection: 'users', - } -); - -UserSchema.plugin(timestamps); - -UserSchema.index({ createdAt: 1, updatedAt: 1 }); - -export const User = mongoose.model('User', UserSchema); -export const UserTC = composeWithMongoose(User); \ No newline at end of file diff --git a/server/src/resolvers.js b/server/src/resolvers.js new file mode 100644 index 0000000..9af9d6c --- /dev/null +++ b/server/src/resolvers.js @@ -0,0 +1,41 @@ +import Product from './models/product.js'; +// import User from './resolvers/User.js'; +// import Appointment from './resolvers/Appointment.js'; +// import Follow from './resolvers/Follow.js'; +// import Query from './resolvers/Query.js'; +// import Mutation from './resolvers/Mutation.js'; +// import Subscription from './resolvers/Subscription.js'; +export const resolvers = { + Query: { + async allProducts() { + return await Product.find(); + }, + + // async feed(parent, args, context, info) { + + // const where = args.filter + // ? { + // OR: [ + // { title: { contains: args.filter } }, + // { description: { contains: args.filter } } + // ] + // } + // : {}; + // console.log(context.mongo); + // const appointments = await context.mongo.appointment.findMany({ + // where, + // skip: args.skip, + // take: args.take, + // orderBy: args.orderBy + // }); + + // const count = await context.mongo.appointment.count({ where }); + + // return { + // id: 'main-feed', + // appointments, + // count + // }; + // } + } +}; \ No newline at end of file diff --git a/server/src/schema.graphql b/server/src/schema.graphql index 5bf97ad..a03e02a 100644 --- a/server/src/schema.graphql +++ b/server/src/schema.graphql @@ -6,6 +6,7 @@ type Query { take: Int orderBy: AppointmentOrderByInput ): Feed! + allProducts: [Product] # users: [User!]! } @@ -64,6 +65,12 @@ type Appointment { createdAt: DateTime! } +type Product { + id: ID! + title: String! + qty: Integer +} + type Follow { id: ID! appointment: Appointment! @@ -81,4 +88,5 @@ enum Sort { desc } +scalar Integer scalar DateTime diff --git a/server/src/schema.js b/server/src/schema.js new file mode 100644 index 0000000..9293723 --- /dev/null +++ b/server/src/schema.js @@ -0,0 +1,45 @@ +import { + makeExecutableSchema +} from 'graphql-tools'; +import { + resolvers +} from './resolvers.js'; + +import fs from 'fs'; +import path from 'path'; + +const moduleURL = new URL(import.meta.url); +const __dirname = path.dirname(moduleURL.pathname); + +const typeDefs = fs.readFileSync( + path.join(__dirname, 'schema.graphql'), + 'utf8' +); +const schema = makeExecutableSchema({ + typeDefs, + // resolvers +}); +export default schema; + + +// import { +// makeExecutableSchema +// } from 'graphql-tools'; +// import { +// resolvers +// } from './resolvers.js'; +// const typeDefs = ` +// type Product { +// _id: ID! +// title: String! +// qty: Int +// } +// type Query { +// allProducts: [Product] +// } +// `; +// const schema = makeExecutableSchema({ +// typeDefs, +// resolvers +// }); +// export default schema; \ No newline at end of file diff --git a/server/src/schema/appointment.js b/server/src/schema/appointment.js deleted file mode 100644 index 552dfd9..0000000 --- a/server/src/schema/appointment.js +++ /dev/null @@ -1,51 +0,0 @@ -import { Appointment, AppointmentTC } from '../models/appointment.js'; - -async function feed(parent, args, context, info) { - - const where = args.filter - ? { - OR: [ - { title: { contains: args.filter } }, - { description: { contains: args.filter } } - ] - } - : {}; - console.log(context.mongo); - const appointments = await context.mongo.appointment.findMany({ - where, - skip: args.skip, - take: args.take, - orderBy: args.orderBy - }); - - const count = await context.mongo.appointment.count({ where }); - - return { - id: 'main-feed', - appointments, - count - }; -} - -const AppointmentQuery = { - appointmentById: AppointmentTC.getResolver('findById'), - appointmentByIds: AppointmentTC.getResolver('findByIds'), - appointmentOne: AppointmentTC.getResolver('findOne'), - appointmentMany: AppointmentTC.getResolver('findMany'), - appointmentCount: AppointmentTC.getResolver('count'), - appointmentConnection: AppointmentTC.getResolver('connection'), - appointmentPagination: AppointmentTC.getResolver('pagination'), -}; - -const AppointmentMutation = { - appointmentCreateOne: AppointmentTC.getResolver('createOne'), - appointmentCreateMany: AppointmentTC.getResolver('createMany'), - appointmentUpdateById: AppointmentTC.getResolver('updateById'), - appointmentUpdateOne: AppointmentTC.getResolver('updateOne'), - appointmentUpdateMany: AppointmentTC.getResolver('updateMany'), - appointmentRemoveById: AppointmentTC.getResolver('removeById'), - appointmentRemoveOne: AppointmentTC.getResolver('removeOne'), - appointmentRemoveMany: AppointmentTC.getResolver('removeMany'), -}; - -export { AppointmentQuery, AppointmentMutation }; \ No newline at end of file diff --git a/server/src/schema/user.js b/server/src/schema/user.js deleted file mode 100644 index bc3e3c7..0000000 --- a/server/src/schema/user.js +++ /dev/null @@ -1,24 +0,0 @@ -import { User, UserTC } from '../models/user.js'; - -const UserQuery = { - userById: UserTC.getResolver('findById'), - userByIds: UserTC.getResolver('findByIds'), - userOne: UserTC.getResolver('findOne'), - userMany: UserTC.getResolver('findMany'), - userCount: UserTC.getResolver('count'), - userConnection: UserTC.getResolver('connection'), - userPagination: UserTC.getResolver('pagination'), -}; - -const UserMutation = { - userCreateOne: UserTC.getResolver('createOne'), - userCreateMany: UserTC.getResolver('createMany'), - userUpdateById: UserTC.getResolver('updateById'), - userUpdateOne: UserTC.getResolver('updateOne'), - userUpdateMany: UserTC.getResolver('updateMany'), - userRemoveById: UserTC.getResolver('removeById'), - userRemoveOne: UserTC.getResolver('removeOne'), - userRemoveMany: UserTC.getResolver('removeMany'), -}; - -export { UserQuery, UserMutation }; \ No newline at end of file