Skip to content

Commit fce78b4

Browse files
committed
refactor(ElasticApiParser): Parse jsdoc once and generate only what you need
1 parent 45605f3 commit fce78b4

File tree

6 files changed

+1093
-289
lines changed

6 files changed

+1093
-289
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ graphql-compose-elasticsearch
1010
Some parts are not ready yet. More features will be landed in near future.
1111
- [x] ElasticApiParser
1212
- [x] Mapping converter to GraphQL types
13-
- [ ] Exporting of generated GraphQL types
13+
- [x] Extended definition for `search.body` according to Query DSL
14+
- [x] Exporting of generated GraphQL field configs for Elastic methods
15+
- [ ] Improve `search.body` Query DSL by field names from provided Mapping
1416
- [ ] Helper methods/resolvers for `graphql-compose`
1517
- [ ] Docs
16-
- [ ] Declarations for output (can not find proper source/format for Elastic responses. Please open PR with link if you have such)
18+
- [ ] Declarations for output (can not find proper source/format for Elastic responses. Please open issue with a link if you have such)
1719

1820
## Live demos
1921
[Introspection of Elasticsearch API via Graphiql](https://graphql-compose.herokuapp.com/elasticsearch/)

examples/differentVersions/index.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ const generatedSchema = new GraphQLSchema({
1616
description: 'Elastic v5.0',
1717
type: new GraphQLObjectType({
1818
name: 'Elastic50',
19-
fields: new ElasticApiParser({ version: '5_0', prefix: 'Elastic50' }).run(),
19+
fields: new ElasticApiParser({
20+
version: '5_0',
21+
prefix: 'Elastic50',
22+
}).generateFieldMap(),
2023
}),
2124
args: {
2225
host: {
@@ -25,7 +28,8 @@ const generatedSchema = new GraphQLSchema({
2528
},
2629
},
2730
resolve: (src, args, context) => {
28-
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
31+
// eslint-disable-next-line no-param-reassign
32+
context.elasticClient = new elasticsearch.Client({
2933
host: args.host,
3034
apiVersion: '5.0',
3135
log: 'trace',
@@ -38,7 +42,10 @@ const generatedSchema = new GraphQLSchema({
3842
description: 'Elastic v2.4',
3943
type: new GraphQLObjectType({
4044
name: 'Elastic24',
41-
fields: new ElasticApiParser({ version: '2_4', prefix: 'Elastic24' }).run(),
45+
fields: new ElasticApiParser({
46+
version: '2_4',
47+
prefix: 'Elastic24',
48+
}).generateFieldMap(),
4249
}),
4350
args: {
4451
host: {
@@ -47,7 +54,8 @@ const generatedSchema = new GraphQLSchema({
4754
},
4855
},
4956
resolve: (src, args, context) => {
50-
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
57+
// eslint-disable-next-line no-param-reassign
58+
context.elasticClient = new elasticsearch.Client({
5159
host: args.host,
5260
apiVersion: '2.4',
5361
});
@@ -59,7 +67,10 @@ const generatedSchema = new GraphQLSchema({
5967
description: 'Elastic v1.7',
6068
type: new GraphQLObjectType({
6169
name: 'Elastic17',
62-
fields: new ElasticApiParser({ version: '5_0', prefix: 'Elastic17' }).run(),
70+
fields: new ElasticApiParser({
71+
version: '5_0',
72+
prefix: 'Elastic17',
73+
}).generateFieldMap(),
6374
}),
6475
args: {
6576
host: {
@@ -68,7 +79,8 @@ const generatedSchema = new GraphQLSchema({
6879
},
6980
},
7081
resolve: (src, args, context) => {
71-
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
82+
// eslint-disable-next-line no-param-reassign
83+
context.elasticClient = new elasticsearch.Client({
7284
host: args.host,
7385
apiVersion: '1.7',
7486
});
@@ -80,17 +92,21 @@ const generatedSchema = new GraphQLSchema({
8092
});
8193

8294
const server = express();
83-
server.use('/', graphqlHTTP({
84-
schema: generatedSchema,
85-
graphiql: true,
86-
context: {
87-
// elasticClient: new elasticsearch.Client({
88-
// host: 'http://localhost:9200',
89-
// apiVersion: '5.0',
90-
// log: 'trace',
91-
// }),
92-
},
93-
}));
95+
server.use(
96+
'/',
97+
graphqlHTTP({
98+
schema: generatedSchema,
99+
graphiql: true,
100+
context: {
101+
// // OR YOU MAY DEFINE elasticClient GLOBALLY
102+
// elasticClient: new elasticsearch.Client({
103+
// host: 'http://localhost:9200',
104+
// apiVersion: '5.0',
105+
// log: 'trace',
106+
// }),
107+
},
108+
})
109+
);
94110

95111
server.listen(expressPort, () => {
96112
console.log(`The server is running at http://localhost:${expressPort}/`);

examples/elastic50/index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ const generatedSchema = new GraphQLSchema({
1616
description: 'Elastic v5.0',
1717
type: new GraphQLObjectType({
1818
name: 'Elastic50',
19-
fields: new ElasticApiParser({ version: '5_0', prefix: 'Elastic50' }).run(),
19+
fields: new ElasticApiParser({
20+
version: '5_0',
21+
prefix: 'Elastic50',
22+
}).generateFieldMap(),
2023
}),
2124
args: {
2225
host: {
@@ -25,7 +28,8 @@ const generatedSchema = new GraphQLSchema({
2528
},
2629
},
2730
resolve: (src, args, context) => {
28-
context.elasticClient = new elasticsearch.Client({ // eslint-disable-line no-param-reassign
31+
// eslint-disable-next-line no-param-reassign
32+
context.elasticClient = new elasticsearch.Client({
2933
host: args.host,
3034
apiVersion: '5.0',
3135
log: 'trace',
@@ -38,17 +42,20 @@ const generatedSchema = new GraphQLSchema({
3842
});
3943

4044
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-
}));
45+
server.use(
46+
'/',
47+
graphqlHTTP({
48+
schema: generatedSchema,
49+
graphiql: true,
50+
context: {
51+
// elasticClient: new elasticsearch.Client({
52+
// host: 'http://localhost:9200',
53+
// apiVersion: '5.0',
54+
// log: 'trace',
55+
// }),
56+
},
57+
})
58+
);
5259

5360
server.listen(expressPort, () => {
5461
console.log(`The server is running at http://localhost:${expressPort}/`);

0 commit comments

Comments
 (0)