-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.express-graphql-http.js
More file actions
57 lines (50 loc) · 1.29 KB
/
1.express-graphql-http.js
File metadata and controls
57 lines (50 loc) · 1.29 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
import express from 'express';
import cors from 'cors';
import { createHandler } from 'graphql-http/lib/use/express';
import { buildSchema } from 'graphql'
const schema = `#graphql
type Query {
hello: String!
schema: String!
random: Float!
isDev: Boolean! @deprecated(reason: "process.env variable does not seem to be set!")
rollDice(numDice: Int = 3, numSides: Int): [Int]
}
`
const rootValue = {
hello: () => {
return "Express & graphql-http server"
},
schema: () => {
return schema
},
random: () => {
return Math.random()
},
isDev: () => {
return process.env.ENV === 'development'
},
rollDice: ({ numDice, numSides }) => {
let output = []
for (let i = 0; i < numDice; i++) {
output.push(1 + Math.floor(Math.random() * (numSides || 6)))
}
return output
},
}
const app = express();
app.use(cors());
app.all('/graphql', createHandler({
schema: buildSchema(schema),
rootValue
}));
app.listen({ port: 4000 });
console.log('🚀 GraphQL server listening to port 4000');
console.log('Test URL:');
console.log('http://localhost:4000/graphql');
console.log('');
console.log('Test "{ hello }" with Postman!');
console.log('Test "{ hello }" with the React /frontend');
console.log('cd frontend');
console.log('npm install');
console.log('npm start');