Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (66 loc) · 1.46 KB
/
Copy pathindex.js
File metadata and controls
76 lines (66 loc) · 1.46 KB
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
const express = require("express");
const graphqlHTTP = require("express-graphql");
const { buildSchema } = require("graphql");
// data source
const { courses } = require("./data.json");
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
course(id: Int!): Course
courses(lang: String): [Course]
}
type Mutation
{
updateCourse(id: Int!, lang: String!): Course
}
type Course
{
id:Int,
title:String,
author: String,
language: String,
url:String
}
`);
// Functions
let getCourse = args => {
let id = args.id;
return courses.filter(course => {
return course.id == id;
})[0];
};
let getCourses = args => {
if (args.lang) {
console.log("Entro aqui " + args.lang);
let lang = args.lang;
return courses.filter(course => course.language === lang);
} else {
return courses;
}
};
let updateCourse = ({ id, lang }) => {
courses.map(course => {
if (course.id === id) {
course.language = lang;
return course;
}
});
return courses.filter(course => course.id == id)[0];
};
// The root provides a resolver function for each API endpoint
var root = {
course: getCourse,
courses: getCourses,
updateCourse: updateCourse
};
var app = express();
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true
})
);
app.listen(4000);
console.log("Running a GraphQL API server at http://localhost:4000/graphql");