-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (58 loc) · 1.83 KB
/
index.js
File metadata and controls
74 lines (58 loc) · 1.83 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const http = require('http');
const express = require('express');
const mongoose = require('mongoose');
const { ApolloServer } = require('apollo-server-express');
const { createUploadLink } = require('apollo-upload-client');
const { setContext } = require('apollo-link-context');
const {
introspectSchema,
makeExecutableSchema,
makeRemoteExecutableSchema,
mergeSchemas,
} = require('graphql-tools');
const fetch = require('node-fetch');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const { JWT_SECRET, MONGODB_URI, SERVER_PORT } = process.env;
const authLink = setContext((request, previousContext) => {
if (previousContext.graphqlContext && previousContext.graphqlContext.req) {
return {
headers: {
'Authorization': previousContext.graphqlContext.req.headers.authorization,
},
};
}
return null;
}).concat(createUploadLink({ uri: 'http://localhost:4001/graphql', fetch }));
const startServer = async () => {
const uploadSchema = makeExecutableSchema({ typeDefs, resolvers });
const authRemoteSchema = await introspectSchema(authLink);
const authSchema = makeRemoteExecutableSchema({
schema: authRemoteSchema,
link: authLink,
});
const schema = mergeSchemas({
schemas: [ authSchema, uploadSchema ],
});
const app = express();
const server = new ApolloServer({ schema, context: req => ({
...req,
})});
server.applyMiddleware({ app });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
return await httpServer.listen(SERVER_PORT);
};
mongoose.connect(MONGODB_URI, {
useCreateIndex: true,
useNewUrlParser: true,
})
.then(() => {
startServer().then(() => {
console.log(`Server start at ${SERVER_PORT}`);
});
// httpServer.listen(SERVER_PORT);
})
.catch(err => {
console.log(err);
});