UNPKG

graphql-compose-elasticsearch

Version:
74 lines 3.23 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRecordITC = void 0; const ElasticApiParser_1 = __importDefault(require("../ElasticApiParser")); const UpdateByIdOutput_1 = require("../types/UpdateByIdOutput"); const utils_1 = require("../utils"); function createUpdateByIdResolver(opts) { const { fieldMap, sourceTC, schemaComposer } = opts; if (!fieldMap || !fieldMap._all) { throw new Error('opts.fieldMap for Resolver updateById() should be fieldMap of FieldsMapByElasticType type.'); } if (!sourceTC || sourceTC.constructor.name !== 'ObjectTypeComposer') { throw new Error('opts.sourceTC for Resolver updateById() should be instance of ObjectTypeComposer.'); } const parser = new ElasticApiParser_1.default({ elasticClient: opts.elasticClient, prefix: opts.prefix, }); const updateByIdFC = parser.generateFieldConfig('update', { index: opts.elasticIndex, type: opts.elasticType, _source: true, }); const argsConfigMap = { id: 'String!', record: getRecordITC(opts).getTypeNonNull(), }; const type = (0, UpdateByIdOutput_1.getUpdateByIdOutputTC)(opts); return schemaComposer.createResolver({ type, name: 'updateById', kind: 'mutation', args: argsConfigMap, resolve: (rp) => __awaiter(this, void 0, void 0, function* () { const { source, args, context, info } = rp; args.body = { doc: Object.assign({}, args.record), }; delete args.record; const res = yield updateByIdFC.resolve(source, args, context, info); const { _index, _type, _id, _version, result, get } = res || {}; const { _source } = get || {}; return Object.assign({ _id, _index, _type, _version, result }, _source); }), }); } exports.default = createUpdateByIdResolver; function getRecordITC(opts) { const name = (0, utils_1.getTypeName)('Record', {}); const description = (0, utils_1.desc)(`The record from Elastic Search`); return opts.getOrCreateITC(name, () => ({ name, description, fields: Object.assign({}, opts.fieldMap._all), })); } exports.getRecordITC = getRecordITC; //# sourceMappingURL=updateById.js.map