Skip to content

Commit 85d2fca

Browse files
committed
🎉 First version
1 parent 9984b36 commit 85d2fca

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

app.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
const {graphql} = require('graphql');
4+
const expressGraphql = require('express-graphql');
5+
const AV = require('leanengine');
6+
const parseLeancloudHeaders = require('leanengine/middleware/parse-leancloud-headers');
7+
8+
const prepareSchema = require('./lib/schema');
9+
10+
const schemaReady = prepareSchema();
11+
12+
const expressGraphqlReady = schemaReady.then( schema => {
13+
return expressGraphql({
14+
schema: schema,
15+
graphiql: true,
16+
});
17+
});
18+
19+
const app = express();
20+
21+
app.use(AV.express());
22+
23+
app.use('/graphql', (req, res, next) => {
24+
expressGraphqlReady.done( expressGraphqlMiddleware => {
25+
expressGraphqlMiddleware(req, res, next);
26+
});
27+
});
28+
29+
app.use(parseLeancloudHeaders(AV, {restrict: false}));
30+
31+
app.use(bodyParser.text({
32+
type: 'application/graphql'
33+
}));
34+
35+
app.get('/', (req, res) => {
36+
res.redirect('/graphql');
37+
})
38+
39+
app.post('/', (req, res) => {
40+
schemaReady.done( schema => {
41+
return graphql(schema, req.body, {}, {
42+
authOptions: {
43+
sessionToken: req.sessionToken,
44+
}
45+
}).then( result => {
46+
res.json(result);
47+
});
48+
});
49+
});
50+
51+
module.exports = app;

lib/schema.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const request = require('request-promise');
2+
const {GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLList, GraphQLFloat, GraphQLBoolean, GraphQLInt} = require('graphql');
3+
const _ = require('lodash');
4+
const AV = require('leanengine');
5+
6+
const appId = process.env.LEANCLOUD_APP_ID;
7+
const appKey = process.env.LEANCLOUD_APP_KEY;
8+
const masterKey = process.env.LEANCLOUD_APP_MASTER_KEY;
9+
10+
const LCTypeMapping = {
11+
String: GraphQLString,
12+
Number: GraphQLFloat,
13+
Boolean: GraphQLBoolean,
14+
15+
// TODO
16+
Object: GraphQLString,
17+
Array: GraphQLString,
18+
Date: GraphQLString,
19+
Relation: GraphQLString,
20+
Pointer: GraphQLString
21+
}
22+
23+
module.exports = function prepareSchema() {
24+
return request({
25+
url: 'https://api.leancloud.cn/1.1/schemas',
26+
json: true,
27+
headers: {
28+
'X-LC-Id': appId,
29+
'X-LC-Key': `${masterKey},master`
30+
}
31+
}).then( cloudSchemas => {
32+
return new GraphQLSchema({
33+
query: new GraphQLObjectType({
34+
name: 'LeanStorage',
35+
fields: _.mapValues(cloudSchemas, (schema, className) => {
36+
return {
37+
name: className,
38+
type: new GraphQLList(new GraphQLObjectType({
39+
name: className,
40+
fields: _.mapValues(schema, (definition, field) => {
41+
return {
42+
type: LCTypeMapping[definition.type],
43+
resolve: (source, args, context, info) => {
44+
return source.get(field);
45+
}
46+
}
47+
})
48+
})),
49+
args: {
50+
ascending: {
51+
type: GraphQLString
52+
},
53+
descending: {
54+
type: GraphQLString
55+
},
56+
limit: {
57+
type: GraphQLInt
58+
},
59+
skip: {
60+
type: GraphQLInt
61+
}
62+
},
63+
resolve: (source, args, {authOptions}, info) => {
64+
return new AV.Query(className).find(authOptions);
65+
}
66+
};
67+
})
68+
})
69+
});
70+
});
71+
};

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "leancloud-graphql",
3+
"version": "0.1.0",
4+
"description": "Third party GraphQL support for LeanCloud, running on LeanEngine",
5+
"engines": {
6+
"node": "6.x"
7+
},
8+
"dependencies": {
9+
"body-parser": "^1.15.2",
10+
"express": "^4.14.0",
11+
"express-graphql": "^0.6.1",
12+
"graphql": "^0.8.2",
13+
"leanengine": "^1.2.3",
14+
"lodash": "^4.17.2",
15+
"request": "^2.79.0",
16+
"request-promise": "^4.1.1"
17+
},
18+
"license": "MIT",
19+
"devDependencies": {}
20+
}

server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const AV = require('leanengine');
2+
3+
AV.init({
4+
appId: process.env.LEANCLOUD_APP_ID,
5+
appKey: process.env.LEANCLOUD_APP_KEY,
6+
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY
7+
});
8+
9+
AV.Cloud.useMasterKey();
10+
11+
const app = require('./app');
12+
const port = process.env.LEANCLOUD_APP_PORT || 3000;
13+
14+
app.listen(port, () => {
15+
console.log('SniperaaS is started on', port);
16+
});

0 commit comments

Comments
 (0)