UNPKG

simple-graphql

Version:

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

56 lines 2.74 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 lodash_1 = __importDefault(require("lodash")); const index_1 = require("../index"); exports.default = { name: 'genHasOneLink', defaultOptions: { enable: true }, priority: 99, description: 'Gen `HasOneLink` for Schema', applyToSchema: function hasOneFieldsConfig(schema, options, schemas) { if (schema instanceof index_1.SGSchema) { lodash_1.default.forOwn(schema.config.associations.hasOne, (config, key) => { if (config.hidden) { return; } schema.links({ [key]: { hookOptions: config.hookOptions, output: { type: config.target }, description: config.description, dependentFields: ['id'], resolve: function (root, args, context, info, sgContext) { return __awaiter(this, void 0, void 0, function* () { if (root[key] !== undefined) { return root[key]; } else { const dbModel = sgContext.models[config.target]; return dbModel.findOneForGraphQL({ where: Object.assign(Object.assign({}, Object.assign({}, (config.scope || {}))), { [config.foreignKey || config.foreignField + 'Id']: root.id }) }, context, info); } }); } } }); }); } } }; //# sourceMappingURL=genHasOneLinkPlugin.js.map