Last active
August 3, 2019 13:50
-
-
Save arminyahya/b5805742e3363e484c3a2903f2804461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var express_graphql = require('express-graphql'); | |
var { buildSchema } = require('graphql'); | |
// GraphQL schema | |
var schema = buildSchema(` | |
type Query { | |
movie(id: Int!): Movie | |
movies(rate: String): [Movie] | |
}, | |
type Movie { | |
id: Int | |
title: String | |
rate: String | |
year: Int | |
} | |
`); | |
var MoviesList = [ | |
{ | |
id: 1, | |
title: 'Avengers: Endgame', | |
rate: '8.7', | |
Year: 2019 | |
}, | |
{ | |
id: 2, | |
title: 'John Wick: Chapter 3 - Parabellum', | |
rate: '7.9', | |
year: 2019 | |
}, | |
{ | |
id: 3, | |
title: 'Once Upon a Time ... in Hollywood', | |
rate: '8.5', | |
year: 2019 | |
}, | |
{ | |
id: 4, | |
title: 'Jurassic World: Fallen Kingdom', | |
rate: '6.5', | |
year: 2018 | |
} | |
] | |
var getMovie = function(args) { | |
var id = args.id; | |
return MoviesList.filter(movie => { | |
return movie.id == id; | |
})[0]; | |
} | |
var getMovies = function(args) { | |
if (args.rate) { | |
var rate = args.rate; | |
return MoviesList.filter(movie => movie.rate === rate); | |
} else { | |
return MoviesList; | |
} | |
} | |
var root = { | |
movie: getMovie, | |
movies: getMovies | |
}; | |
// 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')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment