This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
101 lines (92 loc) · 2.22 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict'
const koa = require('koa');
const co = require('co');
const mount = require('koa-mount');
const db = require('./models');
// CORS
const cors = require('kcors');
// GraphQL Needed
const graphqlHTTP = require('koa-graphql');
const graphql = require('graphql');
const graphSequel = require('graphql-sequelize');
const graphqlModel = require('./graphql');
// Koa APP
const app = koa();
/**
* Schema Database
* TODO:
* - Split this part of code
*/
let schema = new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name: 'RootQueryType',
fields: {
wines: {
type: new graphql.GraphQLList(graphqlModel.Wine.type),
// args will automatically be mapped to `where`
args: {
id: {
type: graphql.GraphQLInt
},
name: {
type: graphql.GraphQLString
},
limit: {
type: graphql.GraphQLInt
},
order: {
type: graphql.GraphQLString
}
},
resolve: graphSequel.resolver(db.Wine, {
include: false // disable auto including of associations based on AST - default: true
})
},
appellation: {
type: graphqlModel.Appellation.type,
args: {
id: {
description: 'id of the appellation',
type: graphql.GraphQLInt
},
name: {
description: 'name of the appellation',
type: graphql.GraphQLString
}
},
resolve: graphSequel.resolver(db.Appellation, {
include: false
})
}
},
}),
mutation: graphqlModel.Wine.mutation
});
/**
* CORS SUPPORT
* origin: *
* allowMethods: GET,HEAD,PUT,POST,DELETE
*/
app.use(cors());
/**
* Mount graphQL route with schema
*/
app.use(mount('/graphql', graphqlHTTP({
schema: schema,
graphiql: true
})));
/**
* Start the application
* Test if DB is synchronized and Listen on port 3000
*/
co(function *(){
const connection = yield db.sequelize.sync();
return connection
}).then(function (connection) {
if(connection){
app.listen(3000);
console.log('connected to database and listening on port 3000');
}
}, function (err) {
console.error(err.stack);
});