-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
34 lines (27 loc) · 977 Bytes
/
server.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
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import bodyParser from 'body-parser';
import schema from './data/schema';
import {User} from './data/models/users';
const GRAPHQL_PORT = 3000;
const MONGO_PORT = 27017;
const MONGO_URL = 'localhost';
const dbName = 'graphExample';
let mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(`mongodb://${MONGO_URL}:${MONGO_PORT}/${dbName}`, {
useMongoClient: true
});
const graphQLServer = express();
//graphQL
graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
graphQLServer.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
//rest api instead
graphQLServer.get('/users', (req, res, next) => {
User.find({}).exec((_err, _res) => res.json(_res));
});
graphQLServer.listen(GRAPHQL_PORT, () =>
console.log(
`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`
)
);