-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (42 loc) · 1 KB
/
index.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
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { buildSchema } = require('graphql');
const app = express();
const schema = buildSchema(`
type User {
id: String
name: String
}
type Query {
feed(username: String!): User
}
`);
const root = {
feed: ({ username }) => ({ id: '0123456', name: username})
};
app.use("/graphql", function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue: root,
graphiql: true,
formatError: error => ({
message: error.message,
locations: error.locations,
stack: error.stack,
path: error.path,
}),
}),
);
app.listen(8080, () => {
console.log('Listening on port :8080/graphql'); // eslint-disable-line
});