-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (35 loc) · 1.13 KB
/
server.js
File metadata and controls
44 lines (35 loc) · 1.13 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
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { loadSchemaSync } = require('@graphql-tools/load');
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader');
const graphqlResolver = require('./resolvers');
const server = express();
const PORT = process.env.PORT || 5000;
const schema = makeExecutableSchema({
typeDefs: loadSchemaSync('schemas/**/*.graphql', {
loaders: [new GraphQLFileLoader()],
}),
resolvers: graphqlResolver,
});
server.use((req, res, next) => {
// extract token from req headers
const token = req.header('Authorization') || 'fake';
// TODO: verify token
// TODO: bind user to req
// we can later access isAuthenticated property
// in resolver functions to check
// if the user is authenticated
req.isAuthenticated = Boolean(token);
// call the next middleware
// whether the user is authenticated or not
next();
});
server.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true,
})
);
server.listen(PORT, () => console.log(`Listening on PORT ${PORT}`));