Skip to content

Commit

Permalink
feat: add fetchElasticMapping helper
Browse files Browse the repository at this point in the history
* add fetchElasticMapping helper

* fix lint error

* fix return Promise type

* fix lint error
  • Loading branch information
thejibz authored and nodkz committed Nov 28, 2018
1 parent f831068 commit 5ee582d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ node_modules

coverage
.nyc_output
package-lock.json
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { composeWithElastic } from './composeWithElastic';
export { convertToSourceTC, inputPropertiesToGraphQLTypes } from './mappingConverter';
export { default as ElasticApiParser } from './ElasticApiParser';
export { elasticApiFieldConfig } from './elasticApiFieldConfig';
export { fetchElasticMapping } from './utils';
33 changes: 33 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { TypeStorage } from 'graphql-compose';
import type { ElasticMappingT } from './mappingConverter';

const typeStorage = new TypeStorage();

Expand Down Expand Up @@ -29,3 +30,35 @@ export function reorderKeys<T: Object>(obj: T, names: string[]): T {
});
return { ...orderedFields, ...fields };
}

export type fetchElasticMappingOptsT = {
elasticIndex: string,
elasticType: string,
elasticMapping: ElasticMappingT,
elasticClient: Object,
};

export async function fetchElasticMapping(
opts: fetchElasticMappingOptsT
): Promise<ElasticMappingT> {
if (!opts.elasticIndex || typeof opts.elasticIndex !== 'string') {
throw new Error('Must provide `elasticIndex` string parameter from your Elastic server.');
}

if (!opts.elasticType || typeof opts.elasticType !== 'string') {
throw new Error('Must provide `elasticType` string parameter from your Elastic server.');
}

if (!opts.elasticClient) {
throw new Error(
'Must provide `elasticClient` Object parameter connected to your Elastic server.'
);
}

const elasticMapping = (await opts.elasticClient.indices.getMapping({
index: opts.elasticIndex,
type: opts.elasticType,
}))[opts.elasticIndex].mappings[opts.elasticType];

return elasticMapping;
}

0 comments on commit 5ee582d

Please sign in to comment.