UNPKG

graphql-compose-elasticsearch

Version:
54 lines 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSortITC = void 0; const utils_1 = require("../utils"); const FieldNames_1 = require("./Commons/FieldNames"); const sortableTypes = [ 'byte', 'short', 'integer', 'long', 'double', 'float', 'half_float', 'scaled_float', 'token_count', 'date', 'boolean', 'ip', 'keyword', ]; function getSortITC(opts) { const name = (0, utils_1.getTypeName)('SortEnum', opts); const description = 'Sortable fields from mapping'; if (!opts.fieldMap) { return opts.schemaComposer.getSTC('JSON'); } return opts.schemaComposer.getOrSet(name, () => { const sortableFields = (0, FieldNames_1.getFieldNamesByElasticType)(opts.fieldMap, sortableTypes); if (sortableFields.length === 0) { return opts.schemaComposer.getSTC('JSON'); } const values = { _score: { value: '_score', }, }; sortableFields.forEach((fieldName) => { const dottedName = fieldName.replace(/__/g, '.'); values[`${fieldName}__asc`] = { value: { [dottedName]: 'asc' }, }; values[`${fieldName}__desc`] = { value: { [dottedName]: 'desc' }, }; }); return opts.schemaComposer.createEnumTC({ name, description, values, }); }); } exports.getSortITC = getSortITC; //# sourceMappingURL=Sort.js.map