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