graphql-compose-elasticsearch
Version:
Elastic search via GraphQL
134 lines • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSubFields = exports.inputPropertiesToGraphQLTypes = exports.propertyToSourceGraphQLType = exports.convertToSourceTC = exports.typeMap = void 0;
const graphql_compose_1 = require("graphql-compose");
const Geo_1 = require("./elasticDSL/Commons/Geo");
exports.typeMap = {
text: 'String',
keyword: 'String',
string: 'String',
byte: 'Int',
short: 'Int',
integer: 'Int',
long: 'Int',
double: 'Float',
float: 'Float',
half_float: 'Float',
scaled_float: 'Float',
date: 'Date',
boolean: 'Boolean',
binary: 'Buffer',
token_count: 'Int',
ip: 'String',
geo_point: Geo_1.ElasticGeoPointType,
geo_shape: 'JSON',
object: 'JSON',
nested: '[JSON]',
completion: 'String',
percolator: 'JSON',
};
function convertToSourceTC(schemaComposer, mapping, typeName, opts = {}) {
if (!mapping || !mapping.properties) {
throw new Error('You provide incorrect mapping. It should be an object `{ properties: {} }`');
}
if (!typeName || typeof typeName !== 'string') {
throw new Error('You provide empty name for type. Second argument `typeName` should be non-empty string.');
}
const tc = schemaComposer.createObjectTC({
name: `${opts.prefix || ''}${typeName}${opts.postfix || ''}`,
description: 'Elasticsearch mapping does not contains info about ' +
'is field plural or not. So `propName` is singular and returns value ' +
'or first value from array. ' +
'`propNameA` is plural and returns array of values.',
});
const { properties = {} } = mapping;
const fields = {};
const pluralFields = opts.pluralFields || [];
Object.keys(properties).forEach((sourceName) => {
const fieldName = sourceName.replace(/[^_a-zA-Z0-9]/g, '_');
const gqType = propertyToSourceGraphQLType(schemaComposer, properties[sourceName], `${typeName}${(0, graphql_compose_1.upperFirst)(fieldName)}`, Object.assign(Object.assign({}, opts), { pluralFields: getSubFields(sourceName, pluralFields) }));
if (gqType) {
if (pluralFields.indexOf(sourceName) >= 0) {
fields[fieldName] = {
type: [gqType],
resolve: (source) => {
if (Array.isArray(source[sourceName])) {
return source[sourceName];
}
return [source[sourceName]];
},
};
}
else {
fields[fieldName] = {
type: gqType,
resolve: (source) => {
if (Array.isArray(source[sourceName])) {
return source[sourceName][0];
}
return source[sourceName];
},
};
}
}
});
tc.addFields(fields);
return tc;
}
exports.convertToSourceTC = convertToSourceTC;
function propertyToSourceGraphQLType(schemaComposer, prop, typeName, opts) {
if (!prop || (typeof prop.type !== 'string' && !prop.properties)) {
throw new Error('You provide incorrect Elastic property config.');
}
if (prop.properties) {
return convertToSourceTC(schemaComposer, prop, typeName || '', opts);
}
const type = prop.type;
if (type && exports.typeMap[type]) {
return exports.typeMap[type];
}
return 'JSON';
}
exports.propertyToSourceGraphQLType = propertyToSourceGraphQLType;
function inputPropertiesToGraphQLTypes(prop, fieldName, result = { _all: {} }) {
if (!prop ||
(typeof prop.type !== 'string' && !prop.properties)) {
throw new Error('You provide incorrect Elastic property config.');
}
const { properties } = prop;
if (properties && (0, graphql_compose_1.isObject)(properties)) {
Object.keys(properties).forEach((subFieldName) => {
inputPropertiesToGraphQLTypes(properties[subFieldName], [fieldName, subFieldName].filter((o) => !!o).join('__'), result);
});
return result;
}
const { fields } = prop;
if (fields && (0, graphql_compose_1.isObject)(fields)) {
Object.keys(fields).forEach((subFieldName) => {
inputPropertiesToGraphQLTypes(fields[subFieldName], [fieldName, subFieldName].filter((o) => !!o).join('__'), result);
});
}
if (prop.hasOwnProperty('index') && !prop.index) {
return result;
}
const type = prop.type;
if (typeof type === 'string' && fieldName) {
if (!result[type]) {
const newMap = {};
result[type] = newMap;
}
const graphqlType = exports.typeMap[type] || 'JSON';
result[type][fieldName] = graphqlType;
result._all[fieldName] = graphqlType;
}
return result;
}
exports.inputPropertiesToGraphQLTypes = inputPropertiesToGraphQLTypes;
function getSubFields(fieldName, pluralFields) {
const st = `${fieldName}.`;
return (pluralFields || [])
.filter((o) => typeof o === 'string' && o.startsWith(st))
.map((v) => v.slice(st.length));
}
exports.getSubFields = getSubFields;
//# sourceMappingURL=mappingConverter.js.map