forked from deshiknaves/cypress-msw-interceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql-server.js
100 lines (91 loc) · 2.37 KB
/
graphql-server.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const express = require('express')
const cors = require('cors')
const { graphqlHTTP } = require('express-graphql')
const { buildSchema } = require('graphql') // GraphQL schema
var schema = buildSchema(`
type Query {
course(id: Int!): Course
courses(topic: String): [Course]
}
type Mutation {
updateCourseTopic(id: Int!, topic: String!): Course
}
type Course {
id: Int
title: String
author: String
description: String
topic: String
url: String
}
`)
const coursesData = [
{
id: 1,
title: 'The Complete Node.js Developer Course',
author: 'Andrew Mead, Rob Percival',
description:
'Learn Node.js by building real-world applications with Node, Express, MongoDB, Mocha, and more!',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs/',
},
{
id: 2,
title: 'Node.js, Express & MongoDB Dev to Deployment',
author: 'Brad Traversy',
description:
'Learn by example building & deploying real-world Node.js applications from absolute scratch',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs-express-mongodb/',
},
{
id: 3,
title: 'JavaScript: Understanding The Weird Parts',
author: 'Anthony Alicea',
description:
'An advanced JavaScript course for everyone! Scope, closures, prototypes, this, build your own framework, and more.',
topic: 'JavaScript',
url: 'https://codingthesmartway.com/courses/understand-javascript/',
},
]
const getCourse = function (args) {
var id = args.id
return coursesData.filter(course => {
return course.id == id
})[0]
}
const getCourses = function (args) {
if (args.topic) {
var topic = args.topic
return coursesData.filter(course => course.topic === topic)
} else {
return coursesData
}
}
const updateCourseTopic = function ({ id, topic }) {
coursesData.map(course => {
if (course.id === id) {
course.topic = topic
return course
}
})
return coursesData.filter(course => course.id === id)[0]
}
const root = {
course: getCourse,
courses: getCourses,
updateCourseTopic,
}
const app = express()
app.use(cors())
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}),
)
app.listen(4000, () =>
console.log('Express GraphQL Server Now Running On localhost:4000/graphql'),
)