123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- var Sequelize = require('sequelize');
- var sequelize = new Sequelize('test', '', '',
- {
- host: 'localhost',
- dialect: 'mysql',
- pool: {
- max: 5,
- min: 0,
- idle: 10000
- },
-
- }
- );
- 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');
- 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
-
- 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
- }
- var root = {
- post: getPost,
- posts: getPosts,
- comments: getPostComments,
- subComments: getSubComments,
- createPost,
- createComment,
- };
- var app = express();
- app.use(cors({origin: 'http://localhost:3000',
- credentials: true}))
- app.use(cookieSession({
- name: 'session',
- keys: ['SuperSecretKey'],
-
-
- maxAge: 24 * 60 * 60 * 1000
- }))
- 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'));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|