|
| 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 | +}); |
0 commit comments