-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (50 loc) · 1.38 KB
/
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
const express = require('express')
const { graphqlHTTP } = require('express-graphql')
const { buildSchema } = require('graphql')
const features = require('./features.json')
const cors = require('cors');
const schema = buildSchema(`
type Query {
hello(name: String): String!
page_title: String!
page_subtitle: String
features(id: Int): [Feature!]!
}
type Feature {
id: Int!
title: String!
description: String
image: String
}
type Mutation {
set_feature_description(id: Int!, description: String!): Int!
}
`)
const root = {
hello: ({name}) => `Hello ${name || 'anonymous'}`,
page_title: () => 'Just some of the features',
page_subtitle: () => 'Here\'s just a glimpse at some of the amazing things you can do using the platform.',
features: ({ id }) =>
features.filter(w => !id || id === w.id),
set_feature_description: ({ id, description }) => {
let count = 0
features.forEach((w) => {
if (w.id === id) {
w.description = description
count++
}
})
return count
}
}
const app = express()
app.use(cors())
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true
}))
const port = 4000;
app.listen(port, () => {
console.info(`Running a GraphQL API server at http://localhost:${port}/graphql`)
})