Skip to content

Commit d653b74

Browse files
committed
feat: Add example graphql server
1 parent b44fc66 commit d653b74

File tree

4 files changed

+355
-4
lines changed

4 files changed

+355
-4
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
*.log
3-
src
3+
src
4+
example

example/index.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import express from 'express';
2+
import graphqlHTTP from 'express-graphql';
3+
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql';
4+
import elasticsearch from 'elasticsearch';
5+
import ElasticApiParser from '../scripts/apiParser/ElasticApiParser';
6+
7+
const expressPort = process.env.port || process.env.PORT || 9201;
8+
9+
const elasticClient = new elasticsearch.Client({
10+
host: 'http://localhost:9200',
11+
apiVersion: '5.0',
12+
log: 'trace',
13+
});
14+
15+
const generatedSchema = new GraphQLSchema({
16+
query: new GraphQLObjectType({
17+
name: 'Query',
18+
// see node_modules/elasticsearch/src/lib/apis/ for available versions
19+
fields: {
20+
elastic50: {
21+
description: 'Elastic v5.0',
22+
type: new GraphQLObjectType({
23+
name: 'Elastic50',
24+
fields: new ElasticApiParser({ version: '5_0', prefix: 'Elastic50' }).run(),
25+
}),
26+
args: {
27+
host: {
28+
type: GraphQLString,
29+
defaultValue: 'http://user:pass@localhost:9200',
30+
},
31+
},
32+
resolve: (src, args, context) => {
33+
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
34+
host: args.host,
35+
apiVersion: '5.0',
36+
});
37+
return {};
38+
},
39+
},
40+
41+
elastic24: {
42+
description: 'Elastic v2.4',
43+
type: new GraphQLObjectType({
44+
name: 'Elastic24',
45+
fields: new ElasticApiParser({ version: '2_4', prefix: 'Elastic24' }).run(),
46+
}),
47+
args: {
48+
host: {
49+
type: GraphQLString,
50+
defaultValue: 'http://user:pass@localhost:9200',
51+
},
52+
},
53+
resolve: (src, args, context) => {
54+
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
55+
host: args.host,
56+
apiVersion: '2.4',
57+
});
58+
return {};
59+
},
60+
},
61+
62+
elastic17: {
63+
description: 'Elastic v1.7',
64+
type: new GraphQLObjectType({
65+
name: 'Elastic17',
66+
fields: new ElasticApiParser({ version: '5_0', prefix: 'Elastic17' }).run(),
67+
}),
68+
args: {
69+
host: {
70+
type: GraphQLString,
71+
defaultValue: 'http://user:pass@localhost:9200',
72+
},
73+
},
74+
resolve: (src, args, context) => {
75+
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
76+
host: args.host,
77+
apiVersion: '1.7',
78+
});
79+
return {};
80+
},
81+
},
82+
},
83+
}),
84+
});
85+
86+
const server = express();
87+
server.use('/', graphqlHTTP({
88+
schema: generatedSchema,
89+
graphiql: true,
90+
context: {
91+
// elasticClient,
92+
},
93+
}));
94+
95+
server.listen(expressPort, () => {
96+
console.log(`The server is running at http://localhost:${expressPort}/`);
97+
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"eslint-plugin-flowtype": "^2.30.0",
4444
"eslint-plugin-import": "^2.2.0",
4545
"eslint-plugin-prettier": "^2.0.0",
46+
"express": "^4.15.2",
47+
"express-graphql": "^0.6.3",
4648
"flow-bin": "^0.41.0",
4749
"graphql": "^0.9.1",
4850
"graphql-compose": "^1.14.0",

0 commit comments

Comments
 (0)