Skip to content

Commit b45b769

Browse files
committed
refactor: code style with improved graphql-compose syntax
1 parent a6967ea commit b45b769

File tree

15 files changed

+316
-370
lines changed

15 files changed

+316
-370
lines changed

src/ElasticApiParser.js

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44
import dox from 'dox';
55
import fs from 'fs';
66
import path from 'path';
7-
import { GraphQLJSON, upperFirst, TypeComposer, type ComposeFieldConfigMap } from 'graphql-compose';
87
import {
9-
GraphQLString,
10-
GraphQLFloat,
11-
GraphQLBoolean,
12-
GraphQLObjectType,
13-
GraphQLEnumType,
14-
GraphQLNonNull,
15-
} from 'graphql-compose/lib/graphql';
16-
import type {
17-
GraphQLArgumentConfig,
18-
GraphQLFieldConfigMap,
19-
GraphQLFieldConfigArgumentMap,
20-
GraphQLInputType,
21-
} from 'graphql-compose/lib/graphql';
8+
upperFirst,
9+
TypeComposer,
10+
EnumTypeComposer,
11+
type ComposeFieldConfigMap,
12+
type ComposeFieldConfigArgumentMap,
13+
type ComposeArgumentConfig,
14+
type ComposeArgumentConfigAsObject,
15+
} from 'graphql-compose';
2216
import { reorderKeys } from './utils';
2317

2418
export type ElasticParamConfigT = {
@@ -67,7 +61,7 @@ export type ElasticApiParserOptsT = {
6761

6862
export default class ElasticApiParser {
6963
cachedEnums: {
70-
[fieldName: string]: { [valsStringified: string]: GraphQLEnumType },
64+
[fieldName: string]: { [valsStringified: string]: EnumTypeComposer },
7165
};
7266

7367
apiVersion: string;
@@ -258,7 +252,7 @@ export default class ElasticApiParser {
258252
return result;
259253
}
260254

261-
generateFieldMap(): GraphQLFieldConfigMap<*, *> {
255+
generateFieldMap(): ComposeFieldConfigMap<any, any> {
262256
const result = {};
263257
Object.keys(this.parsedSource).forEach(methodName => {
264258
result[methodName] = this.generateFieldConfig(methodName);
@@ -293,7 +287,7 @@ export default class ElasticApiParser {
293287
const argMap = this.settingsToArgMap(argsSettings, argsDescriptions);
294288

295289
return {
296-
type: GraphQLJSON,
290+
type: 'JSON',
297291
description,
298292
args: argMap,
299293
// eslint-disable-next-line no-unused-vars
@@ -322,10 +316,11 @@ export default class ElasticApiParser {
322316
paramCfg: ElasticParamConfigT,
323317
fieldName: string,
324318
description?: ?string
325-
): GraphQLArgumentConfig {
326-
const result: GraphQLArgumentConfig = {
319+
): ComposeArgumentConfig {
320+
const result: { ...ComposeArgumentConfigAsObject } = {
327321
type: this.paramTypeToGraphQL(paramCfg, fieldName),
328322
};
323+
329324
if (paramCfg.default) {
330325
result.defaultValue = paramCfg.default;
331326
} else if (fieldName === 'format') {
@@ -336,40 +331,40 @@ export default class ElasticApiParser {
336331
result.description = description;
337332
}
338333

339-
return result;
334+
return (result: any);
340335
}
341336

342-
paramTypeToGraphQL(paramCfg: ElasticParamConfigT, fieldName: string): GraphQLInputType {
337+
paramTypeToGraphQL(paramCfg: ElasticParamConfigT, fieldName: string): EnumTypeComposer | string {
343338
switch (paramCfg.type) {
344339
case 'string':
345-
return GraphQLString;
340+
return 'String';
346341
case 'boolean':
347-
return GraphQLBoolean;
342+
return 'Boolean';
348343
case 'number':
349-
return GraphQLFloat;
344+
return 'Float';
350345
case 'time':
351-
return GraphQLString;
346+
return 'String';
352347
case 'list':
353-
return GraphQLJSON;
348+
return 'JSON';
354349
case 'enum':
355350
if (Array.isArray(paramCfg.options)) {
356351
return this.getEnumType(fieldName, paramCfg.options);
357352
}
358-
return GraphQLString;
353+
return 'String';
359354
case undefined:
360355
// some fields may not have type definition in API file,
361356
// eg '@param {anything} params.operationThreading - ?'
362-
return GraphQLJSON;
357+
return 'JSON';
363358
default:
364359
// console.log(
365360
// // eslint-disable-line
366361
// `New type '${paramCfg.type}' in elastic params setting for field ${fieldName}.`
367362
// );
368-
return GraphQLJSON;
363+
return 'JSON';
369364
}
370365
}
371366

372-
getEnumType(fieldName: string, vals: mixed[]): GraphQLEnumType {
367+
getEnumType(fieldName: string, vals: mixed[]): EnumTypeComposer {
373368
const key = fieldName;
374369
const subKey = JSON.stringify(vals);
375370

@@ -403,7 +398,7 @@ export default class ElasticApiParser {
403398
if (postfix === 0) postfix = '';
404399
else postfix = `_${postfix}`;
405400

406-
this.cachedEnums[key][subKey] = new GraphQLEnumType({
401+
this.cachedEnums[key][subKey] = EnumTypeComposer.create({
407402
name: `${this.prefix}Enum_${upperFirst(fieldName)}${postfix}`,
408403
values,
409404
});
@@ -415,13 +410,13 @@ export default class ElasticApiParser {
415410
settingsToArgMap(
416411
settings: ?ElasticCaSettingsT,
417412
descriptions: ElasticParsedArgsDescriptionsT = {}
418-
): GraphQLFieldConfigArgumentMap {
413+
): ComposeFieldConfigArgumentMap {
419414
const result = {};
420415
const { params, urls, url, method, needBody } = settings || {};
421416

422417
if (method === 'POST' || method === 'PUT') {
423418
result.body = {
424-
type: needBody ? new GraphQLNonNull(GraphQLJSON) : GraphQLJSON,
419+
type: needBody ? 'JSON!' : 'JSON',
425420
};
426421
}
427422

@@ -452,7 +447,7 @@ export default class ElasticApiParser {
452447
return result;
453448
}
454449

455-
reassembleNestedFields(fields: ComposeFieldConfigMap<any, any>): GraphQLFieldConfigMap<*, *> {
450+
reassembleNestedFields(fields: ComposeFieldConfigMap<any, any>): ComposeFieldConfigMap<any, any> {
456451
const result = {};
457452
Object.keys(fields).forEach(k => {
458453
const names = k.split('.');
@@ -461,7 +456,7 @@ export default class ElasticApiParser {
461456
} else {
462457
if (!result[names[0]]) {
463458
result[names[0]] = {
464-
type: new GraphQLObjectType({
459+
type: TypeComposer.create({
465460
name: `${this.prefix}_${upperFirst(names[0])}`,
466461
fields: (() => {}: any),
467462
}),
@@ -470,7 +465,7 @@ export default class ElasticApiParser {
470465
},
471466
};
472467
}
473-
TypeComposer.create(result[names[0]].type).setField(names[1], fields[k]);
468+
result[names[0]].type.setField(names[1], fields[k]);
474469
}
475470
});
476471

0 commit comments

Comments
 (0)