Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions graphql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions graphql/Model/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

mongoose.connection.openUri('mongodb://nahtanoy:132435@phase2-shard-00-00-fjtwn.mongodb.net:27017,phase2-shard-00-01-fjtwn.mongodb.net:27017,phase2-shard-00-02-fjtwn.mongodb.net:27017/test?ssl=true&replicaSet=PHASE2-shard-0&authSource=admin', (err) => {
if (err) console.log('Database Not Connected');
console.log('Database Connected');
})

const MovieSchema = Schema({
title: String,
overview: String,
poster_path: String,
status: String,
popularity: Number,
tag: String,
})

const MoviesModel = mongoose.model('Movies', MovieSchema)

module.exports = MoviesModel;
149 changes: 149 additions & 0 deletions graphql/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const app = require('express')()
const graphqlHTTP = require('express-graphql')
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLString,
GraphQLList,
GraphQLInt,
GraphQLFloat
} = require('graphql');
const logger = require('morgan')

const MoviesModel = require('./Model')

const MoviesType = new GraphQLObjectType({
name: 'Movies',
fields: {
_id: {type: GraphQLString},
title: {type: GraphQLString},
overview: {type: GraphQLString},
popularity: {type: GraphQLFloat},
poster_path: {type: GraphQLString},
status: {type: GraphQLString},
tag: {type: GraphQLString}
}
})

const MovieInputType = new GraphQLInputObjectType({
name: 'MovieInput',
fields: {
title: {type: GraphQLString},
overview: {type: GraphQLString},
popularity: {type: GraphQLFloat},
poster_path: {type: GraphQLString},
status: {type: GraphQLString},
tag: {type: GraphQLString}
}
})

const MovieEditType = new GraphQLInputObjectType({
name: 'MovieEdit',
fields: {
_id: {type: GraphQLString},
title: {type: GraphQLString},
overview: {type: GraphQLString},
popularity: {type: GraphQLFloat},
poster_path: {type: GraphQLString},
status: {type: GraphQLString},
tag: {type: GraphQLString}
}
})

const MovieDeleteType = new GraphQLInputObjectType({
name: 'MovieDelete',
fields: {
_id: {type: GraphQLString}
}
})

const AppQuery = new GraphQLObjectType({
name: 'Hello',
fields: {
movies: {
type: new GraphQLList(MoviesType),
resolve: () => {
return MoviesModel.find()
}
}
}
})

const AppMutation = new GraphQLObjectType({
name: 'appMutation',
fields: {
addMovie: {
type: new GraphQLList(MoviesType),
args: {
movieParam: {
name: 'movie params',
type: MovieInputType
}
},
resolve: async (root, args) => {
const {movieParam} = args
await MoviesModel.create(movieParam)
let movie = await MoviesModel.find()
return movie
}
},
editMovie: {
type: new GraphQLList(MoviesType),
args: {
editMovie: {
name: 'edit movie',
type: MovieEditType
}
},
resolve: async (root, args) => {
const {editMovie} = args
const id = editMovie._id
await MoviesModel.update({_id: id}, {
title: editMovie.title,
overview: editMovie.overview,
popularity: editMovie.popularity,
tag: editMovie.tag,
poster_path: editMovie.poster_path,
status: editMovie.status
})
let dataMovie = await MoviesModel.find()
return dataMovie
}
},
deleteMovie: {
type: new GraphQLList(MoviesType),
args: {
removeMovie: {
name: 'removeMovie',
type: MovieDeleteType
}
},
resolve: async (root, args) => {
const {removeMovie} = args
const id = removeMovie._id
await MoviesModel.findByIdAndRemove(id)
let movies = await MoviesModel.find()
return movies
}
}
}
})

const appSchema = new GraphQLSchema({
query: AppQuery,
mutation: AppMutation
})

app.use(logger('dev'));

app.use('/graphql', graphqlHTTP({
schema: appSchema,
graphiql:true
}))
app.use('/', (req, res) => {
res.send('Hello Connection')
})


app.listen(4040, () => console.log('=========Connect========'))
16 changes: 16 additions & 0 deletions graphql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "graphql",
"version": "1.0.0",
"description": "using graphql",
"main": "index.js",
"author": "setiadijoe",
"license": "MIT",
"dependencies": {
"axios": "^0.17.1",
"express": "^4.16.2",
"express-graphql": "^0.6.11",
"graphql": "^0.12.3",
"mongoose": "^4.13.9",
"morgan": "^1.9.0"
}
}
Loading