Skip to content

Commit f36c827

Browse files
committed
refactor(Example): Elastic50 proxy and multi-version proxy
1 parent d1994d3 commit f36c827

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

example/index.js renamed to examples/differentVersions/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import graphqlHTTP from 'express-graphql';
33
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql';
44
import elasticsearch from 'elasticsearch';
5-
import ElasticApiParser from '../src/ElasticApiParser';
5+
import ElasticApiParser from '../../src/ElasticApiParser'; // or import { ElasticApiParser } from 'graphql-compose-elasticsearch';
66

77
const expressPort = process.env.port || process.env.PORT || 9201;
88

examples/elastic50/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 '../../src/ElasticApiParser'; // or import { ElasticApiParser } from 'graphql-compose-elasticsearch';
6+
7+
const expressPort = process.env.port || process.env.PORT || 9201;
8+
9+
const generatedSchema = new GraphQLSchema({
10+
query: new GraphQLObjectType({
11+
name: 'Query',
12+
fields: {
13+
// see node_modules/elasticsearch/src/lib/apis/ for available versions
14+
15+
elastic50: {
16+
description: 'Elastic v5.0',
17+
type: new GraphQLObjectType({
18+
name: 'Elastic50',
19+
fields: new ElasticApiParser({ version: '5_0', prefix: 'Elastic50' }).run(),
20+
}),
21+
args: {
22+
host: {
23+
type: GraphQLString,
24+
defaultValue: 'http://user:pass@localhost:9200',
25+
},
26+
},
27+
resolve: (src, args, context) => {
28+
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
29+
host: args.host,
30+
apiVersion: '5.0',
31+
log: 'trace',
32+
});
33+
return {};
34+
},
35+
},
36+
},
37+
}),
38+
});
39+
40+
const server = express();
41+
server.use('/', graphqlHTTP({
42+
schema: generatedSchema,
43+
graphiql: true,
44+
context: {
45+
// elasticClient: new elasticsearch.Client({
46+
// host: 'http://localhost:9200',
47+
// apiVersion: '5.0',
48+
// log: 'trace',
49+
// }),
50+
},
51+
}));
52+
53+
server.listen(expressPort, () => {
54+
console.log(`The server is running at http://localhost:${expressPort}/`);
55+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"build": "npm-run-all build:*",
7171
"build:lib": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
7272
"build:flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
73+
"example": "nodemon -e js --exec ./node_modules/.bin/babel-node ./examples/elastic50/index.js",
7374
"coverage": "jest --coverage",
7475
"lint": "eslint src test *.js",
7576
"test": "jest",

0 commit comments

Comments
 (0)