Ivan Asmer 6 vuotta sitten
vanhempi
commit
b5014a198f
2 muutettua tiedostoa jossa 44 lisäystä ja 3 poistoa
  1. 41 3
      index.js
  2. 3 0
      package.json

+ 41 - 3
index.js

@@ -1,5 +1,5 @@
 var Sequelize = require('sequelize');
-var sequelize = new Sequelize('test', 'root', '',
+var sequelize = new Sequelize('test', '', '',
     {
         host: 'localhost',
         dialect: 'mysql',
@@ -68,9 +68,9 @@ async function fillDB(){
     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);
-    post1.addComment(comment1)
 
 
     var comment2 = await Comment.create({text: 'test comment for second post'})
@@ -78,7 +78,7 @@ async function fillDB(){
     var comment22 = await Comment.create({text: 'test comment 2 for first comment of second post'})
 
     comment2.addComment(comment21);
-    comment2.addComment(comment22);
+    comment21.addComment(comment22);
     post2.addComment(comment2)
 
     var tag1 = await Tag.create({title: 'tag1'})
@@ -128,6 +128,44 @@ async function getData(){
 
 getData()
 
+var express = require('express');
+var express_graphql = require('express-graphql');
+var { buildSchema } = require('graphql');
+// GraphQL schema
+var schema = buildSchema(`
+    type Query {
+        post(id: Int!): Post
+    }
+    type Post {
+        id: Int
+        title: String
+        text:  String
+        age:   String
+    }
+`);
+
+function getPost(args){
+    let id = args.id
+    return Post.findById(id)
+}
+
+
+// Root resolver
+var root = {
+    post: getPost
+};
+
+
+
+// Create an express server and a GraphQL endpoint
+var app = express();
+app.use('/graphql', express_graphql({
+    schema: schema,
+    rootValue: root,
+    graphiql: true
+}));
+app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
+
 
 //sequelize.sync().then(function(){
     //return Post.create(

+ 3 - 0
package.json

@@ -9,6 +9,9 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "express": "^4.16.3",
+    "express-graphql": "^0.6.12",
+    "graphql": "^0.13.2",
     "mysql2": "^1.5.3",
     "sequelize": "^4.37.10"
   }