inversify-graphql
Version:
Builds dependency-inverted GraphQL schemas with InversifyJS
149 lines • 5.9 kB
JavaScript
"use strict";
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 __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.extensibleSchema = exports.InversifyExtensibleSchema = exports.InversifyExtensibleNode = void 0;
const inversify_1 = require("inversify");
const build_1 = require("./build");
const object_builder_1 = require("./object-builder");
const interfaces_private_1 = require("./interfaces-private");
const schema_builder_1 = require("./schema-builder");
const utils_1 = require("./utils");
let InversifyExtensibleNode = class InversifyExtensibleNode {
constructor() {
this.extensions = [];
this.useParentExtensions = false;
}
for(name) {
this.typeName = name;
return this;
}
/** Merge the given field list definitions in the current node */
merge(...fields) {
this.extensions.push(...fields);
return this;
}
buildType() {
if (!this.extensions.length)
return null;
// create a temp class
const that = this;
class Temp extends object_builder_1.InversifyObjectTypeBuilder {
constructor() {
super();
super.extensions = that.useParentExtensions ? 'noDirect' : 'none';
}
config() {
const fieldsMap = {};
// augment fields with extensions
for (const ext of that.extensions) {
let thisMap = this.builders.get(ext).map();
if (typeof thisMap === 'function')
thisMap = thisMap();
for (const fname of Object.keys(thisMap)) {
fieldsMap[fname] = thisMap[fname];
}
}
// return the type
return {
name: that.typeName,
fields: fieldsMap,
};
}
}
return utils_1.named(Temp, this.typeName + '_Extensible');
}
};
InversifyExtensibleNode = __decorate([
inversify_1.injectable()
], InversifyExtensibleNode);
exports.InversifyExtensibleNode = InversifyExtensibleNode;
class InversifyExtensibleSchema {
constructor(name, container) {
this.nodes = new Map();
this.parents = [];
this.orphanTypes = [];
const c = this.container = new inversify_1.Container();
c.parent = container;
c.bind(inversify_1.Container).toConstantValue(c);
c.bind(object_builder_1.ExtensibleSchemaSymbol).toConstantValue(this);
this.query = this.get(name + 'Queries');
this.mutation = this.get(name + 'Mutations');
this.subscription = this.get(name + 'Subscriptions');
for (const q of [this.query, this.mutation, this.subscription]) {
q.useParentExtensions = true;
}
}
create(typeName) {
return this.container.resolve(InversifyExtensibleNode)
.for(typeof typeName === 'string' ? typeName : null);
}
get(typeToExtend) {
let node = this.nodes.get(typeToExtend);
if (!node) {
this.nodes.set(typeToExtend, node = this.create(typeToExtend));
}
return node;
}
addTypes(...additionalTypes) {
this.orphanTypes.push(...additionalTypes);
return this;
}
getNoCreate(extendedType, ctor, which) {
if (which === 'none')
return [];
const ret = [];
// push extensions of parents first
for (const p of this.parents) {
ret.push(...p.getNoCreate(extendedType, ctor, 'all'));
}
// get parent extensions for roots
for (const q of ['query', 'mutation', 'subscription']) {
if (this[q].typeName === extendedType) {
for (const p of this.parents)
ret.push(p[q]);
}
}
// finally, override all previous extensions by extensions in this schema
if (which !== 'noDirect') {
ret.push(this.nodes.get(extendedType));
ret.push(this.nodes.get(ctor));
}
return ret.filter(x => !!x);
}
concat(...otherSchema) {
this.parents.push(...otherSchema);
return this;
}
build() {
const that = this;
class Temp extends schema_builder_1.InversifySchemaBuilder {
schema() {
return {
query: that.query.buildType(),
mutation: that.mutation.buildType(),
subscription: that.subscription.buildType(),
types: that.orphanTypes,
};
}
}
__decorate([
inversify_1.inject(interfaces_private_1.ITypeCache),
__metadata("design:type", interfaces_private_1.ITypeCache)
], Temp.prototype, "builders", void 0);
return build_1.inversifySchema(this.container, Temp);
}
}
exports.InversifyExtensibleSchema = InversifyExtensibleSchema;
function extensibleSchema(name, container) {
return new InversifyExtensibleSchema(name, container);
}
exports.extensibleSchema = extensibleSchema;
//# sourceMappingURL=extensible.js.map