Skip to content

Commit b90a1f4

Browse files
committed
fix(Flowtype): Resolve all Flow warnings
1 parent 6d35e47 commit b90a1f4

File tree

8 files changed

+42
-87
lines changed

8 files changed

+42
-87
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"peerDependencies": {
2828
"elasticsearch": ">=12.0.0 || >=13.0.0",
29-
"graphql-compose": ">=1.19.2"
29+
"graphql-compose": ">=1.19.3"
3030
},
3131
"devDependencies": {
3232
"babel-cli": "^6.24.1",
@@ -46,7 +46,7 @@
4646
"express-graphql": "^0.6.6",
4747
"flow-bin": "^0.47.0",
4848
"graphql": "^0.10.1",
49-
"graphql-compose": "^1.19.2",
49+
"graphql-compose": "^1.19.3",
5050
"jest": "^20.0.4",
5151
"jest-babel": "^1.0.1",
5252
"npm-run-all": "^4.0.1",

src/ElasticApiParser.js

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
GraphQLFieldConfig,
1313
GraphQLFieldConfigMap,
1414
GraphQLFieldConfigArgumentMap,
15-
GraphQLFieldMap,
1615
GraphQLInputType,
1716
} from "graphql/type/definition"; // eslint-disable-line
1817

@@ -89,8 +88,7 @@ export default class ElasticApiParser {
8988
opts.elasticClient.transport._config.apiVersion) ||
9089
'_default';
9190
const apiFilePath = path.resolve(
92-
opts.elasticApiFilePath ||
93-
ElasticApiParser.findApiVersionFile(this.apiVersion)
91+
opts.elasticApiFilePath || ElasticApiParser.findApiVersionFile(this.apiVersion)
9492
);
9593
const source = ElasticApiParser.loadApiFile(apiFilePath);
9694
this.parsedSource = ElasticApiParser.parseSource(source);
@@ -109,9 +107,7 @@ export default class ElasticApiParser {
109107
try {
110108
code = ElasticApiParser.loadFile(absolutePath);
111109
} catch (e) {
112-
throw new Error(
113-
`Cannot load Elastic API source file from ${absolutePath}`
114-
);
110+
throw new Error(`Cannot load Elastic API source file from ${absolutePath}`);
115111
}
116112
return ElasticApiParser.cleanUpSource(code);
117113
}
@@ -121,9 +117,7 @@ export default class ElasticApiParser {
121117
try {
122118
code = ElasticApiParser.loadFile(absolutePath);
123119
} catch (e) {
124-
throw new Error(
125-
`Cannot load Elastic API file with avaliable versions from ${absolutePath}`
126-
);
120+
throw new Error(`Cannot load Elastic API file with avaliable versions from ${absolutePath}`);
127121
}
128122
return code;
129123
}
@@ -135,29 +129,21 @@ export default class ElasticApiParser {
135129

136130
// parsing elasticsearch module 13.x and above
137131
// get '5.3'() { return require('./5_3'); },
138-
const re = new RegExp(
139-
`\\'${version}\\'\\(\\).*require\\(\\'(.+)\\'\\)`,
140-
'gi'
141-
);
132+
const re = new RegExp(`\\'${version}\\'\\(\\).*require\\(\\'(.+)\\'\\)`, 'gi');
142133
const match = re.exec(apiListCode);
143134
if (match && match[1]) {
144135
return path.resolve(apiFolder, `${match[1]}.js`);
145136
}
146137

147138
// parsing elasticsearch module 12.x and below
148139
// '5.0': require('./5_0'),
149-
const re12 = new RegExp(
150-
`\\'${version}\\':\\srequire\\(\\'(.+)\\'\\)`,
151-
'gi'
152-
);
140+
const re12 = new RegExp(`\\'${version}\\':\\srequire\\(\\'(.+)\\'\\)`, 'gi');
153141
const match12 = re12.exec(apiListCode);
154142
if (match12 && match12[1]) {
155143
return path.resolve(apiFolder, `${match12[1]}.js`);
156144
}
157145

158-
throw new Error(
159-
`Can not found Elastic version '${version}' in ${apiListFile}`
160-
);
146+
throw new Error(`Can not found Elastic version '${version}' in ${apiListFile}`);
161147
}
162148

163149
static cleanUpSource(code: string): string {
@@ -172,9 +158,7 @@ export default class ElasticApiParser {
172158
return codeCleaned;
173159
}
174160

175-
static parseParamsDescription(
176-
doxItemAST: any
177-
): { [fieldName: string]: string } {
161+
static parseParamsDescription(doxItemAST: any): { [fieldName: string]: string } {
178162
const descriptions = {};
179163
if (Array.isArray(doxItemAST.tags)) {
180164
doxItemAST.tags.forEach(tag => {
@@ -184,9 +168,7 @@ export default class ElasticApiParser {
184168
const name = ElasticApiParser.cleanupParamName(tag.name);
185169
if (!name) return;
186170

187-
descriptions[name] = ElasticApiParser.cleanupDescription(
188-
tag.description
189-
);
171+
descriptions[name] = ElasticApiParser.cleanupDescription(tag.description);
190172
});
191173
}
192174
return descriptions;
@@ -258,9 +240,7 @@ export default class ElasticApiParser {
258240
// method description
259241
let description;
260242
if (item.description && item.description.full) {
261-
description = ElasticApiParser.cleanupDescription(
262-
item.description.full
263-
);
243+
description = ElasticApiParser.cleanupDescription(item.description.full);
264244
}
265245

266246
const elasticMethod = ElasticApiParser.getMethodName(item.ctx.string);
@@ -310,12 +290,9 @@ export default class ElasticApiParser {
310290
throw new Error(`Elastic search method '${methodName}' does not exists.`);
311291
}
312292

313-
const {
314-
description,
315-
argsSettings,
316-
argsDescriptions,
317-
elasticMethod,
318-
} = this.parsedSource[methodName];
293+
const { description, argsSettings, argsDescriptions, elasticMethod } = this.parsedSource[
294+
methodName
295+
];
319296

320297
const argMap = this.settingsToArgMap(argsSettings, argsDescriptions);
321298

@@ -366,10 +343,7 @@ export default class ElasticApiParser {
366343
return result;
367344
}
368345

369-
paramTypeToGraphQL(
370-
paramCfg: ElasticParamConfigT,
371-
fieldName: string
372-
): GraphQLInputType {
346+
paramTypeToGraphQL(paramCfg: ElasticParamConfigT, fieldName: string): GraphQLInputType {
373347
switch (paramCfg.type) {
374348
case 'string':
375349
return GraphQLString;
@@ -458,11 +432,7 @@ export default class ElasticApiParser {
458432

459433
if (params) {
460434
Object.keys(params).forEach(k => {
461-
const fieldConfig = this.paramToGraphQLArgConfig(
462-
params[k],
463-
k,
464-
descriptions[k]
465-
);
435+
const fieldConfig = this.paramToGraphQLArgConfig(params[k], k, descriptions[k]);
466436
if (fieldConfig) {
467437
result[k] = fieldConfig;
468438
}
@@ -475,11 +445,7 @@ export default class ElasticApiParser {
475445
urlList.forEach(item => {
476446
if (item.req) {
477447
Object.keys(item.req).forEach(k => {
478-
const fieldConfig = this.paramToGraphQLArgConfig(
479-
item.req[k],
480-
k,
481-
descriptions[k]
482-
);
448+
const fieldConfig = this.paramToGraphQLArgConfig(item.req[k], k, descriptions[k]);
483449
if (fieldConfig) {
484450
result[k] = fieldConfig;
485451
}
@@ -491,7 +457,7 @@ export default class ElasticApiParser {
491457
return result;
492458
}
493459

494-
reassembleNestedFields(fields: GraphQLFieldMap<*, *>): GraphQLFieldMap<*, *> {
460+
reassembleNestedFields(fields: GraphQLFieldConfigMap<*, *>): GraphQLFieldConfigMap<*, *> {
495461
const result = {};
496462
Object.keys(fields).forEach(k => {
497463
const names = k.split('.');
@@ -510,10 +476,7 @@ export default class ElasticApiParser {
510476
},
511477
};
512478
}
513-
TypeComposer.create(result[names[0]].type).setField(
514-
names[1],
515-
fields[k]
516-
);
479+
TypeComposer.create(result[names[0]].type).setField(names[1], fields[k]);
517480
}
518481
});
519482

src/__tests__/ElasticApiParser-test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ describe('ElasticApiParser', () => {
471471
describe('reassembleNestedFields()', () => {
472472
it('should pass single fields', () => {
473473
expect(
474-
// $FlowFixMe
475474
parser.reassembleNestedFields({
476475
field1: { type: GraphQLString },
477476
field2: { type: GraphQLString },
@@ -483,7 +482,6 @@ describe('ElasticApiParser', () => {
483482
});
484483

485484
it('should combine nested field in GraphQLObjectType', () => {
486-
// $FlowFixMe
487485
const reFields = parser.reassembleNestedFields({
488486
'cat.field1': { type: GraphQLString },
489487
'cat.field2': { type: GraphQLString },

src/composeWithElastic.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
/* @flow */
2+
13
import { TypeComposer } from 'graphql-compose';
2-
import {
3-
convertToSourceTC,
4-
inputPropertiesToGraphQLTypes,
5-
} from './mappingConverter';
6-
import type { ElasticMappingT } from './mappingConverter';
4+
import { convertToSourceTC, inputPropertiesToGraphQLTypes } from './mappingConverter';
75
import createSearchResolver from './resolvers/search';
86
import createSearchConnectionResolver from './resolvers/searchConnection';
97

8+
import type { ElasticMappingT } from './mappingConverter';
9+
1010
export type composeWithElasticOptsT = {
1111
graphqlTypeName: string,
1212
elasticIndex: string,
1313
elasticType: string,
1414
elasticMapping: ElasticMappingT,
1515
elasticClient: Object,
1616
pluralFields?: string[],
17+
prefix?: ?string,
18+
postfix?: ?string,
1719
};
1820

19-
export function composeWithElastic(
20-
opts: composeWithElasticOptsT = {}
21-
): TypeComposer {
21+
export function composeWithElastic(opts: composeWithElasticOptsT): TypeComposer {
2222
if (!opts) {
2323
throw new Error('Opts is required argument for composeWithElastic()');
2424
}
@@ -46,7 +46,10 @@ export function composeWithElastic(
4646
'Opts.graphqlTypeName is required property for generated GraphQL Type name in composeWithElastic()'
4747
);
4848
}
49-
opts.prefix = opts.graphqlTypeName; // eslint-disable-line
49+
50+
if (!opts.prefix) {
51+
opts.prefix = opts.graphqlTypeName; // eslint-disable-line
52+
}
5053

5154
if (opts.pluralFields && !Array.isArray(opts.pluralFields)) {
5255
throw new Error(
@@ -56,11 +59,7 @@ export function composeWithElastic(
5659
}
5760

5861
const fieldMap = inputPropertiesToGraphQLTypes(opts.elasticMapping);
59-
const sourceTC = convertToSourceTC(
60-
opts.elasticMapping,
61-
opts.graphqlTypeName,
62-
opts
63-
);
62+
const sourceTC = convertToSourceTC(opts.elasticMapping, opts.graphqlTypeName, opts);
6463

6564
const searchR = createSearchResolver(fieldMap, sourceTC, opts);
6665
const searchConnectionR = createSearchConnectionResolver(searchR, opts);

src/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
/* @flow */
2+
13
export { composeWithElastic } from './composeWithElastic';
2-
export {
3-
convertToSourceTC,
4-
inputPropertiesToGraphQLTypes,
5-
} from './mappingConverter';
4+
export { convertToSourceTC, inputPropertiesToGraphQLTypes } from './mappingConverter';
65
export { default as ElasticApiParser } from './ElasticApiParser';
76
export { elasticApiFieldConfig } from './elasticApiFieldConfig';

src/resolvers/search.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { getSearchBodyITC, prepareBodyInResolve } from '../elasticDSL/SearchBody
99
import { getSearchOutputTC } from '../types/SearchOutput';
1010

1111
export type ElasticSearchResolverOpts = {
12-
[name: string]: mixed,
13-
prefix?: string,
12+
prefix?: ?string,
1413
elasticIndex: string,
1514
elasticType: string,
1615
elasticClient: Object,
@@ -121,7 +120,7 @@ export default function createSearchResolver(
121120

122121
const { hits = {} } = projection;
123122

124-
if (typeof hits === 'object') {
123+
if (hits && typeof hits === 'object') {
125124
// Turn on explain if in projection requested this fields:
126125
if (hits._shard || hits._node || hits._explanation) {
127126
args.body.explain = true;

src/utils.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ export function getTypeName(name: string, opts: any): string {
66
return `${(opts && opts.prefix) || 'Elastic'}${name}${(opts && opts.postfix) || ''}`;
77
}
88

9-
export function getOrSetType<T>(
10-
typeName: string,
11-
typeOrThunk: (() => T) | T
12-
): T {
9+
export function getOrSetType<T>(typeName: string, typeOrThunk: (() => T) | T): T {
1310
// $FlowFixMe
1411
const type: T = typeStorage.getOrSet(typeName, typeOrThunk);
1512
return type;
1613
}
1714

1815
// Remove newline multiline in descriptions
1916
export function desc(str: string): string {
20-
return str.replace(/\n\s+/ig, ' ').replace(/^\s+/, '');
17+
return str.replace(/\n\s+/gi, ' ').replace(/^\s+/, '');
2118
}
2219

2320
export function reorderKeys<T: Object>(obj: T, names: string[]): T {

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,9 +2017,9 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6:
20172017
version "1.0.1"
20182018
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
20192019

2020-
graphql-compose@^1.19.2:
2021-
version "1.19.2"
2022-
resolved "https://registry.yarnpkg.com/graphql-compose/-/graphql-compose-1.19.2.tgz#277734f58aa65cd890e461916e17fbe34e4b5c68"
2020+
graphql-compose@^1.19.3:
2021+
version "1.19.3"
2022+
resolved "https://registry.yarnpkg.com/graphql-compose/-/graphql-compose-1.19.3.tgz#cea43697378b4da864d34836bbd36376de71649a"
20232023
dependencies:
20242024
babel-runtime "^6.23.0"
20252025
object-path "^0.11.4"

0 commit comments

Comments
 (0)