Created
July 12, 2020 06:11
-
-
Save mugan86/f481ff23c26b9f2d2edad3cb6e8cd642 to your computer and use it in GitHub Desktop.
Servidor básico con lo mínimo en GraphQL haciendo uso de GraphiQL
This file contains 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
const express = require("express"); | |
const { buildSchema } = require("graphql"); | |
const { graphqlHTTP } = require("express-graphql"); | |
const bodyParser = require('body-parser'); | |
const courses = [ | |
{ | |
id: 1, | |
title: "GraphQL de 0 a experto", | |
views: 29290, | |
}, | |
{ | |
id: 2, | |
title: "Tienda de videojuegos - MEAN+G", | |
views: 122, | |
}, | |
]; | |
const schema = buildSchema(` | |
type Course { | |
id: ID! | |
title: String! | |
views: Int | |
} | |
type Query { | |
getCourses: [Course] | |
} | |
`); | |
// The root provides a resolver function for each API endpoint | |
var root = { | |
getCourses: () => { | |
return courses; | |
} | |
}; | |
const app = express(); | |
// parse application/json | |
app.use(bodyParser.json()) | |
app.use('/graphql', graphqlHTTP({ | |
schema, | |
graphiql: true, | |
rootValue: root | |
})); | |
app.listen(4000, () => { | |
console.log('Server ok in http://localhost:4000/graphql'); | |
}) | |
This file contains 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
{ | |
"name": "graphql-basic", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "npx nodemon index.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.19.0", | |
"express": "^4.17.1", | |
"express-graphql": "^0.11.0", | |
"graphql": "^15.3.0" | |
}, | |
"devDependencies": { | |
"nodemon": "^2.0.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment