UNPKG

simple-graphql

Version:

The simple way to generates GraphQL schemas and Sequelize models from your models definition.

55 lines 2.65 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 }); const dataloader_1 = __importDefault(require("dataloader")); const getFindOptionsKey_1 = __importDefault(require("../../plugin/cache/getFindOptionsKey")); function default_1(options, context, info, path) { return __awaiter(this, void 0, void 0, function* () { const dbModel = this; if (context == null) context = {}; if (!context._SGLoaders) context._SGLoaders = {}; const key = `${dbModel.name}.findOne`; if (!context._SGLoaders[key]) { context._SGLoaders[key] = new dataloader_1.default(function (conditions) { return __awaiter(this, void 0, void 0, function* () { const result = []; for (let cond of conditions) { if (dbModel.withCache) { result.push(yield dbModel.withCache().findOne(cond)); } else { result.push(yield dbModel.findOne(cond)); } } return result; }); }, { maxBatchSize: 1, cacheKeyFn: (k) => (0, getFindOptionsKey_1.default)(dbModel, k) }); } const option = dbModel.resolveQueryOption({ attributes: options.attributes, include: options.include, order: options.order, info: info, path: path }); return context._SGLoaders[key].load(Object.assign(Object.assign({}, options), { include: option.include, attributes: option.attributes, order: option.order })); }); } exports.default = default_1; //# sourceMappingURL=findOneForGraphQL.js.map