repl.it
@426729/

Testing GraphQL + Mongoose

Nodejs

No description

fork
loading
Files
  • index.js
  • index.html
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const express=require("express");
const expressGraphql=require("express-graphql");
const {buildSchema}=require("graphql");
const mongoose=require("mongoose");
const request=require("request");
const assert=require('assert');

mongoose.connect(process.env.DATABASE.replace(/<password>/,process.env.PASSWORD),{useNewUrlParser:true});


const schema=buildSchema(`
type Query {
  actor(name:String):Actor
  movie(name:String):Movie
}
type Mutation {
  makeActor(name:String!):Actor
  makeMovie(name:String!):Movie
  enrollActor(name:String!,movie:String!):Actor
}
type Actor {
  name:String,
  movies:[Movie]
  age:Int
}
type Movie {
  name:String
  actors:[Actor]
  boxOffice:Int
}
`);
__dirname = process.cwd();
const app=express();
app.get("/",(req,res)=>{
  res.sendFile(__dirname+"/index.html");
});
const {ObjectId} = mongoose.Schema.Types;
const Schema=mongoose.Schema;
const db=mongoose.connection;
db.on('error',err=>{
  console.error(err);
});
db.on('open',()=>{
  console.log("opened");
  const actors=new Schema({
    name:String,
    // movies:[Movie],
    age:Number
  });
  actors.methods.toString=function(){
    return `${this.name} - Has starred in ${this.movies[0].name}`;
  }

  const movies=new Schema({
    name:String,
    actors:[{
      type:ObjectId,
      ref:"Actor"
    }],
    boxOffice:Number
  });
  movies.methods.toString=function(){
    return `${this.title} - Starring ${this.actors[0].toString()}`;
  }


  actors.add({
    movies:[{
      type:ObjectId,
      ref:"Movie"
    }]
  });

  const Actor=mongoose.model("Actor",actors);
  console.log("Got Actor first stuff done");
  const Movie=mongoose.model("Movie",movies);
  const root={
    movie:async ({name})=>{
      const res=await Movie.findOne({name});
      if(!res) throw(`Movie by the name of ${name} doesn't exist`);
      return res;
    },
    actor:async ({name})=>{
      const res=await Actor.findOne({name});
      if(!res) throw(`Actor by the name of ${name} doesn't exist`);
      return res;
    }
    // movie:({name})=>Movie.findOne({name},(err,movie)=>
    //   err?console.log(err):movie
    // ),
    // actor:({name})=>Actor.findOne({name},(err,actor)=>
    //   err?console.log(err):actor
    // ),
    // makeMovie:({name})=>new Promise((resolve,reject)=>Movie.findOne({name},(err,movie)=>err||!movie?(new Movie({name,actors:[]}).save((err,movie)=>err?reject(err):resolve(movie))):reject(err||"Movie already exists"))),
    // makeActor:({name})=>new Promise((resolve,reject)=>
    //   Actor.findOne({name},(err,actor)=>err||!actor?(new Actor({name,movies:[]})).save((err,actor)=>err?reject(err):resolve(actor)):reject(err||"Actor already exists"))),
    // enrollActor:({name,movie:movieName})=>{
    //   return new Promise((resolve,reject)=>
    //   Actor.findOne({name},(err,actor)=>{
    //     if(err||!actor) return reject(err||"Actor does not exist");
    //   Movie.findOne({name:movieName},(err,movie)=>{
    //     if(err|!movie) return reject(err||"Movie does not exist");
    //   if(!actor.movies.map(i=>i.name).includes(movieName)){
    //     actor.movies.push(movie);
    //     movie.actors.push(actor);
    //     movie.save((err,movie)=>{
    //       if(err) reject(err);
    //       actor.save((err,actor)=>err?reject(err):resolve(actor))
    //     });
    //   } else return reject("Actor is already enrolled");
    //   });
    //   }))

    // }
  };

  app.use("/graphiql",expressGraphql({
    schema,
    rootValue:root,
    graphiql:true
  }));

});
request("https://postb.in/1560291993962-2315253941342");
const PORT=process.env.PORT||4000;
app.listen(PORT,()=>console.log("Server started at "+PORT));
?