var Sequelize = require('sequelize'); var sequelize = new Sequelize('test', '', '', { host: 'localhost', dialect: 'mysql', pool: { max: 5, min: 0, idle: 10000 }, //logging: false } ); var Post = sequelize.define('post', { title: Sequelize.STRING, text: Sequelize.TEXT, },{ getterMethods: { tagz() { return this.getTags().then(tagz => tagz.map(tag => tag.title)) }, age() { return (((new Date).getTime() - this.createdAt.getTime()) / 3600000).toFixed(0) + ' hrs ago'; } }, }) var Comment = sequelize.define('comment',{ text: Sequelize.TEXT }) Post.hasMany(Comment) Comment.belongsTo(Post) Comment.hasMany(Comment) Comment.belongsTo(Comment) var Tag = sequelize.define('tag', { title: Sequelize.STRING }) Tag.belongsToMany(Post, {through: 'PostTag'}) Post.belongsToMany(Tag, {through: 'PostTag'}) async function fillDB(){ await sequelize.sync() if (await Post.count()){ return; } var post1 = await Post.create( { title: 'First Post', text: 'First Post BLah-blah-blah' }) var post2 = await Post.create( { title: 'Second Post', text: 'Second Post BLah-blah-blah' }) var post3 = await Post.create( { title: 'Third Post', text: 'Third Post BLah-blah-blah' }) var comment1 = await Comment.create({text: 'test comment for first post'}) var comment11 = await Comment.create({text: 'test comment for first comment of first post'}) var comment12 = await Comment.create({text: 'test comment 2 for first comment of first post'}) post1.addComment(comment1) comment1.addComment(comment11); comment1.addComment(comment12); var comment2 = await Comment.create({text: 'test comment for second post'}) var comment21 = await Comment.create({text: 'test comment for first comment of second post'}) var comment22 = await Comment.create({text: 'test comment 2 for first comment of second post'}) comment2.addComment(comment21); comment21.addComment(comment22); post2.addComment(comment2) var tag1 = await Tag.create({title: 'tag1'}) var tag2 = await Tag.create({title: 'tag2'}) var tag3 = await Tag.create({title: 'tag3'}) tag1.addPosts([post1, post3]) post1.addTags([tag2]) post2.addTags([tag2, tag3]) tag3.addPost(post3) } fillDB() var cookieSession = require('cookie-session') var express = require('express') var express = require('express'); const cors = require('cors') var express_graphql = require('express-graphql'); var { buildSchema } = require('graphql'); // GraphQL schema // var schema = buildSchema(` type Query { post(id: Int!): Post comments(id: Int!): [Comment] subComments(id: Int!): [Comment] posts: [Post] } type Mutation { createPost(title: String!, text: String!): Post createComment(postID: Int!, text: String!, commentID: Int): Comment } type Post { id: Int title: String text: String age: String tagz: [String] comments: [Comment] timestamp: Int } type Comment { id: Int text: String age: String commentId: Int } `); async function getPost({id}, {session}){ console.log(session) session.lastId = id; console.log(session) let post = await Post.findOne({ where: { id } }) return post; } async function getPosts(args, {session}){ let posts = await Post.findAll({}) for (let post of posts){ post.timestamp = post.createdAt.getTime()/1000 } return posts; } function getPostComments(args){ let id = args.id return Post.findById(id) .then( post => post.getComments() ) .then( comments => comments.filter( comment => !comment.commentId)) } async function getSubComments(args){ let id = args.id //return Comment.findById(id).then( comment => comment.getComments() ) let comment = await Comment.findById(id) return comment.getComments() } async function createPost({title, text}){ return Post.create({title, text}) } async function createComment({postID, text, commentID}){ let post = await Post.findById(postID) let comment = await Comment.create({text, commentId: commentID}) post.addComment(comment) return comment } // Root resolver var root = { post: getPost, posts: getPosts, comments: getPostComments, subComments: getSubComments, createPost, createComment, }; // Create an express server and a GraphQL endpoint var app = express(); app.use(cors({origin: 'http://localhost:3000', credentials: true})) app.use(cookieSession({ name: 'session', keys: ['SuperSecretKey'], // Cookie Options maxAge: 24 * 60 * 60 * 1000 // 24 hours })) app.use('/graphql', express_graphql(req => ({ schema: schema, rootValue: root, graphiql: true, context: (console.log(req.session),req.session.counter = (req.session.counter || 0) +1, ({session: req.session})) }))); app.listen(4000, '0.0.0.0', () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql')); //query getPost($postID: Int!){ //post(id:$postID){ //text //timestamp //} //} //query getComments($postID: Int!){ //comments(id:$postID){ //text //} //} //query getPostWithComments($postID: Int!){ //post(id:$postID){ //text //comments { //text //age //} //} //} //query getSubComments($commentID: Int!){ //subComments(id:$commentID){ //text //} //} //mutation createPost($title: String!, $text:String!) { //createPost(title: $title, text: $text) { //title //text //} //} //mutation createComment($postID:Int!, $text:String!) { //createComment(postID: $postID, text: $text) { //text //} //}