-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (87 loc) · 2.85 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
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
var graphql = require('graphql');
var graphqlHTTP = require('express-graphql');
var express = require('express');
var Crate = require('cratejs');
var q = require('q');
// define the HTTP endpoint of the GraphQL middleware
var db = module.exports = new Crate({
host: 'localhost',
port: 4200
});
var executeSql = function (stmt) {
var deferred = q.defer();
var query_limit = 10;
db.execute(stmt, [query_limit], function (err, res) {
deferred.resolve(res);
});
return deferred.promise;
};
/**********************************************
*************** GraphQL Schema ****************
***********************************************/
var actorType = new graphql.GraphQLObjectType({
name: 'Actor',
fields: {
id: { type: graphql.GraphQLString },
avatar_url: { type: graphql.GraphQLString },
login: { type: graphql.GraphQLString },
gravatar_id: { type: graphql.GraphQLString },
url : { type: graphql.GraphQLString }
}
});
var repoType = new graphql.GraphQLObjectType({
name: 'Repo',
fields: {
id: { type: graphql.GraphQLString },
name: { type: graphql.GraphQLString },
url: { type: graphql.GraphQLString }
}
});
var schema = new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name: 'Query',
fields: {
actor: {
type: actorType,
args: {
id: { type: graphql.GraphQLString }
},
resolve: function (_, args) {
var res = executeSql("select actor from github where id='" + args.id + "' limit ?");
return res.then(function(data) {
var mydata = data['rows'][0][0];
console.log(mydata);
return mydata;
});
}
},
repo: {
type: repoType,
args: {
name: { type: graphql.GraphQLString },
id: { type: graphql.GraphQLString }
},
resolve: function (_, args) {
if (args.id != null && args.name != null) {
var res = executeSql("select repo from github where repo['id']='" + args.id + "' and repo['name']='" + args.name + "' limit ?");
} else if (args.id != null && args.name == null) {
var res = executeSql("select repo from github where repo['id']='" + args.id + "' limit ?");
} else if (args.id == null && args.name != null) {
var res = executeSql("select repo from github where repo['name']='" + args.name + "' limit ?");
} else {
var res = executeSql("select repo from github limit ?");
}
return res.then(function(data) {
var mydata = data['rows'][0][0];
console.log(mydata);
return mydata;
});
}
}
}
})
});
console.log('Started on http://localhost:3000/graphql');
var app = express()
app.use('/graphql', graphqlHTTP({ schema: schema, retty: true, graphiql: true }));
app.listen(3000);