graphql-compose-elasticsearch
Version:
Elastic search via GraphQL
60 lines • 3.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.composeWithElastic = void 0;
const graphql_compose_1 = require("graphql-compose");
const mappingConverter_1 = require("./mappingConverter");
const search_1 = __importDefault(require("./resolvers/search"));
const searchConnection_1 = __importDefault(require("./resolvers/searchConnection"));
const searchPagination_1 = __importDefault(require("./resolvers/searchPagination"));
const findById_1 = __importDefault(require("./resolvers/findById"));
const updateById_1 = __importDefault(require("./resolvers/updateById"));
const utils_1 = require("./utils");
function composeWithElastic(opts) {
if (!opts) {
throw new Error('Opts is required argument for composeWithElastic()');
}
if (!opts.elasticMapping || !opts.elasticMapping.properties) {
throw new Error('You provide incorrect elasticMapping property. It should be an object `{ properties: {} }`');
}
if (!opts.elasticIndex || typeof opts.elasticIndex !== 'string') {
throw new Error('Third arg for Resolver search() should contain `elasticIndex` string property from your Elastic server.');
}
if (!opts.elasticType || typeof opts.elasticType !== 'string') {
throw new Error('Third arg for Resolver search() should contain `elasticType` string property from your Elastic server.');
}
if (typeof opts.graphqlTypeName !== 'string' || !opts.graphqlTypeName) {
throw new Error('Opts.graphqlTypeName is required property for generated GraphQL Type name in composeWithElastic()');
}
if (!opts.prefix) {
opts.prefix = opts.graphqlTypeName;
}
if (opts.pluralFields && !Array.isArray(opts.pluralFields)) {
throw new Error('Opts.pluralFields should be an Array of strings with field names ' +
'which are plural (you may use dot notation for nested fields).');
}
if (opts.schemaComposer && !(opts.schemaComposer instanceof graphql_compose_1.SchemaComposer)) {
throw new Error('Opts.schemaComposer should be an SchemaComposer instance from graphql-compose package.');
}
const schemaComposer = opts.schemaComposer || graphql_compose_1.schemaComposer;
const fieldMap = (0, mappingConverter_1.inputPropertiesToGraphQLTypes)(opts.elasticMapping);
const sourceTC = (0, mappingConverter_1.convertToSourceTC)(schemaComposer, opts.elasticMapping, opts.graphqlTypeName, opts);
const commonOpts = (0, utils_1.prepareCommonOpts)(schemaComposer, Object.assign(Object.assign({}, opts), { prefix: opts.prefix || 'Es', fieldMap,
sourceTC,
schemaComposer }));
const searchR = (0, search_1.default)(commonOpts);
const searchConnectionR = (0, searchConnection_1.default)(commonOpts, searchR);
const searchPaginationR = (0, searchPagination_1.default)(commonOpts, searchR);
const findByIdR = (0, findById_1.default)(commonOpts);
const updateByIdR = (0, updateById_1.default)(commonOpts);
sourceTC.addResolver(searchR);
sourceTC.addResolver(searchConnectionR);
sourceTC.addResolver(searchPaginationR);
sourceTC.addResolver(findByIdR);
sourceTC.addResolver(updateByIdR);
return sourceTC;
}
exports.composeWithElastic = composeWithElastic;
//# sourceMappingURL=composeWithElastic.js.map