UNPKG

inversify-graphql

Version:

Builds dependency-inverted GraphQL schemas with InversifyJS

88 lines 3.89 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InversifyUnionTypeBuilder = void 0; const inv = __importStar(require("inversify")); const gql = __importStar(require("graphql")); const interfaces_private_1 = require("./interfaces-private"); let InversifyUnionTypeBuilder = class InversifyUnionTypeBuilder { constructor() { this.extensions = 'all'; } build() { if (this.built) return this.built; if (this.building) throw new Error(`The type ${this.constructor.name} is involved in a circlar referenciation loop. If this is intended, please use the thunk version of 'fields' - i.e. "fields: () => ({ /* field definition */ })`); this.building = true; // resolve types // deep copy item (this.config() might return a constant => must not be modified) const cfg = { ...this.config() }; if (typeof cfg.types === 'function') { const cpy = cfg.types; cfg.types = () => [...cpy()]; } else if (cfg.types instanceof Array) { cfg.types = [...cfg.types]; } // map types let types; if (typeof cfg.types === 'function') { const cpy = cfg.types; types = () => cpy().map(x => this.builders.buildType(x)); } else { types = cfg.types.map(x => this.builders.buildType(x)); } // return real object type this.built = new gql.GraphQLUnionType({ ...cfg, types: types, resolveType: async (value, ctx, info) => { const ret = await cfg.resolveType(value, ctx, info); if (!ret) return null; return this.builders.buildType(ret); } }); delete this.building; return this.built; } }; __decorate([ inv.inject(interfaces_private_1.ITypeCache), __metadata("design:type", interfaces_private_1.ITypeCache) ], InversifyUnionTypeBuilder.prototype, "builders", void 0); InversifyUnionTypeBuilder = __decorate([ inv.injectable() ], InversifyUnionTypeBuilder); exports.InversifyUnionTypeBuilder = InversifyUnionTypeBuilder; //# sourceMappingURL=union-builder.js.map