Skip to content

Commit 56c815a

Browse files
committed
feat(Search): Add field searchTyped with described inputs, search now has untyped body
1 parent 74b08e3 commit 56c815a

File tree

8 files changed

+84
-33
lines changed

8 files changed

+84
-33
lines changed

.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
"no-plusplus": 0,
2222
"import/no-extraneous-dependencies": 0,
2323
"import/prefer-default-export": 0,
24+
"prettier/prettier": ["error", {
25+
"printWidth": 80,
26+
"singleQuote": true,
27+
"trailingComma": "es5",
28+
}]
2429
},
2530
"env": {
2631
"jasmine": true,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
graphql-compose-elasticsearch
22
======================
33
[![](https://img.shields.io/npm/v/graphql-compose-elasticsearch.svg)](https://www.npmjs.com/package/graphql-compose-elasticsearch)
4-
[![npm](https://img.shields.io/npm/dt/graphql-compose-elasticsearch.svg)](https://www.npmjs.com/package/graphql-compose-elasticsearch)
4+
[![npm](https://img.shields.io/npm/dt/graphql-compose-elasticsearch.svg)](http://www.npmtrends.com/graphql-compose-elasticsearch)
55
[![Travis](https://img.shields.io/travis/nodkz/graphql-compose-elasticsearch.svg?maxAge=2592000)](https://travis-ci.org/nodkz/graphql-compose-elasticsearch)
66
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
77
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

src/ElasticApiParser.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
GraphQLNonNull,
1414
} from 'graphql';
1515
import { GraphQLJSON, upperFirst, TypeComposer } from 'graphql-compose';
16-
import { getSearchBodyITC } from './ElasticDSL/SearchBody';
16+
import { getSearchBodyITC, prepareSearchArgs } from './ElasticDSL/SearchBody';
1717

1818
import type {
1919
GraphQLArgumentConfig,
@@ -24,7 +24,7 @@ import type {
2424

2525
export type ElasticApiParserOptsT = {
2626
version?:
27-
'5_0'
27+
| '5_0'
2828
| '5_x'
2929
| '2_4'
3030
| '2_3'
@@ -81,15 +81,40 @@ export default class ElasticApiParser {
8181
// from ../../node_modules/elasticsearch/src/lib/apis/VERSION.js
8282
this.version = opts.version || '5_0';
8383
this.prefix = opts.prefix || 'Elastic';
84-
this.elasticApiFilesPath = opts.elasticApiFilesPath || './node_modules/elasticsearch/src/lib/apis/';
84+
this.elasticApiFilesPath = opts.elasticApiFilesPath ||
85+
'./node_modules/elasticsearch/src/lib/apis/';
8586
this.cachedEnums = {};
8687
}
8788

88-
run() {
89+
run(): GraphQLFieldMap<*, *> {
8990
this.cachedEnums = {};
90-
const apiFilePath = path.resolve(this.elasticApiFilesPath, `${this.version}.js`);
91+
const apiFilePath = path.resolve(
92+
this.elasticApiFilesPath,
93+
`${this.version}.js`
94+
);
9195
const source = this.loadApiFile(apiFilePath);
92-
return this.parseSource(source);
96+
return this.addTypedSearch(this.parseSource(source));
97+
}
98+
99+
addTypedSearch(fields: GraphQLFieldMap<*, *>): GraphQLFieldMap<*, *> {
100+
if (fields.search) {
101+
const { type, description, args, resolve } = fields.search;
102+
const searchTyped = {
103+
type,
104+
description,
105+
resolve: prepareSearchArgs(resolve),
106+
// $FlowFixMe
107+
args: Object.assign({}, args, {
108+
body: {
109+
type: getSearchBodyITC({
110+
prefix: this.prefix,
111+
}).getType(),
112+
},
113+
}),
114+
};
115+
return { searchTyped, ...fields };
116+
}
117+
return fields;
93118
}
94119

95120
parseSource(source: string): GraphQLFieldMap<*, *> {
@@ -126,12 +151,6 @@ export default class ElasticApiParser {
126151

127152
const elasticMethod = this.getMethodName(item.ctx.string);
128153

129-
if (elasticMethod === 'search') {
130-
argMap.body.type = getSearchBodyITC({
131-
prefix: this.prefix,
132-
}).getType();
133-
}
134-
135154
result[item.ctx.string] = {
136155
type: GraphQLJSON,
137156
description,
@@ -232,7 +251,7 @@ export default class ElasticApiParser {
232251
};
233252
if (paramCfg.default) {
234253
result.defaultValue = paramCfg.default;
235-
} else if(fieldName === 'format') {
254+
} else if (fieldName === 'format') {
236255
result.defaultValue = 'json';
237256
}
238257

@@ -265,7 +284,9 @@ export default class ElasticApiParser {
265284
// eg '@param {anything} params.operationThreading - ?'
266285
return GraphQLJSON;
267286
default:
268-
console.log(`New type '${paramCfg.type}' in elastic params setting for field ${fieldName}.`); // eslint-disable-line
287+
console.log(
288+
`New type '${paramCfg.type}' in elastic params setting for field ${fieldName}.`
289+
); // eslint-disable-line
269290
return GraphQLJSON;
270291
}
271292
}
@@ -369,7 +390,9 @@ export default class ElasticApiParser {
369390
// $FlowFixMe
370391
fields: () => {},
371392
}),
372-
resolve: () => { return {}; },
393+
resolve: () => {
394+
return {};
395+
},
373396
};
374397
}
375398
TypeComposer.create(result[name[0]].type).setField(name[1], fields[k]);

src/ElasticDSL/Commons/Script.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* @flow */
2+
3+
import { InputTypeComposer } from 'graphql-compose';
4+
import { getTypeName, getOrSetType, desc } from "../../utils";
5+
6+
export function getCommonsScriptITC(opts: mixed = {}): InputTypeComposer {
7+
const name = getTypeName('CommonsScript', opts);
8+
const description = desc(`
9+
The scripting module enables you to use scripts to evaluate custom expressions.
10+
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html)
11+
`);
12+
13+
return getOrSetType(name, () =>
14+
// $FlowFixMe
15+
InputTypeComposer.create({
16+
name,
17+
description,
18+
fields: {
19+
lang: 'String!',
20+
inline: 'String!',
21+
params: 'JSON',
22+
},
23+
})
24+
);
25+
}

src/ElasticDSL/Query/Specialized/Script.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { InputTypeComposer } from 'graphql-compose';
44
import { getTypeName, getOrSetType, desc } from "../../../utils";
5+
import { getCommonsScriptITC } from '../../Commons/Script';
56

67
export function getScriptITC(opts: mixed = {}): InputTypeComposer {
78
const name = getTypeName('QueryScript', opts);
@@ -16,11 +17,7 @@ export function getScriptITC(opts: mixed = {}): InputTypeComposer {
1617
name,
1718
description,
1819
fields: {
19-
script: `input ${getTypeName('QueryScriptScript', opts)} {
20-
inline: String!
21-
lang: String!
22-
params: JSON
23-
}`,
20+
script: () => getCommonsScriptITC(),
2421
},
2522
})
2623
);

src/ElasticDSL/Query/Query.test.js renamed to src/ElasticDSL/__tests__/SearchDSL-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* @flow */
22
import { printSchema, GraphQLSchema, GraphQLObjectType, GraphQLInt } from 'graphql';
3-
import { getQueryITC } from './Query';
3+
import { getQueryITC } from '../Query/Query';
44

5-
describe('Elastic DSL Query', () => {
6-
it('should match', () => {
5+
describe('Elastic Search DSL', () => {
6+
it('Query DSL', () => {
77
const schema = new GraphQLSchema({
88
query: new GraphQLObjectType({
99
name: 'RootQuery',

src/ElasticDSL/Query/__snapshots__/Query.test.js.snap renamed to src/ElasticDSL/__tests__/__snapshots__/SearchDSL-test.js.snap

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Elastic DSL Query should match 1`] = `
3+
exports[`Elastic Search DSL Query DSL 1`] = `
44
"schema {
55
query: RootQuery
66
}
@@ -302,13 +302,7 @@ enum Elastic_QueryQueryStringOperatorEnum_50 {
302302
# A query allowing to define scripts as queries. They are typically used in a
303303
# filter context. [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html)
304304
input Elastic_QueryScript_50 {
305-
script: Elastic_QueryScriptScript_50
306-
}
307-
308-
input Elastic_QueryScriptScript_50 {
309-
inline: String!
310-
lang: String!
311-
params: JSON
305+
script: ElasticCommonsScript
312306
}
313307
314308
# A query that uses the SimpleQueryParser to parse its context. Unlike the regular
@@ -336,6 +330,13 @@ enum Elastic_QuerySimpleQueryStringOperatorEnum_50 {
336330
or
337331
}
338332
333+
# The scripting module enables you to use scripts to evaluate custom expressions. [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html)
334+
input ElasticCommonsScript {
335+
lang: String!
336+
inline: String!
337+
params: JSON
338+
}
339+
339340
# The \`JSON\` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
340341
scalar JSON
341342

src/__tests__/__snapshots__/ElasticApiParser-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Object {
3333
"type": "String",
3434
},
3535
"body": Object {
36-
"type": "ElasticSearchBody",
36+
"type": "JSON",
3737
},
3838
"defaultOperator": Object {
3939
"defaultValue": "OR",

0 commit comments

Comments
 (0)