Skip to content

Instantly share code, notes, and snippets.

@mugan86
Created July 12, 2020 06:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugan86/f481ff23c26b9f2d2edad3cb6e8cd642 to your computer and use it in GitHub Desktop.
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
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');
})
{
"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