-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (46 loc) · 1.65 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
49
50
51
52
53
54
55
56
57
58
const express = require('express');
const app = express();
const MongoDBConnection = require('./db/mongo');
const ListingsCollection = MongoDBConnection.client.db().collection('listings');
const checkAuth = require('./middleware/checkAuthMiddleware');
// GraphQL Imports for Apollo Configuration
const types = require('./graphql/types')
const resolvers = require ('./graphql/resolvers')
const PropertiesAPI = require('./datasources/properties');
const Listings = require('./datasources/listings');
const { ApolloServer } = require('apollo-server-express');
// Build settings object for Apollo Server
let apolloSettings = {
typeDefs: [types],
resolvers: resolvers,
dataSources: () => {
return {
propertiesAPI: new PropertiesAPI(),
listings: new Listings(ListingsCollection)
};
}
};
async function startServer() {
const server = new ApolloServer(apolloSettings);
// Make sure database is started
await MongoDBConnection.connect();
// Then start everything else
await server.start();
// Tell Apollo to use the /graphql route
server.applyMiddleware({ app, path:"/graphql" });
}
startServer();
app.listen({ port: 4000 }, () =>
console.log(`Listening on http://localhost:4000/graphql`)
);
// Define middleware on specific routes for authentication purposes
app.use('/graphql', function (req, res, next) {
checkAuth(req)
.then((auth) => {
if (!auth) {
return res.status(401).send('You must provide a valid bearer token to use this endpoint');
}
// If auth is valid continue through the stack
return next();
});
})