inversify-graphql
Version:
Builds dependency-inverted GraphQL schemas with InversifyJS
167 lines • 5.49 kB
JavaScript
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GBool = exports.GFloat = exports.GInt = exports.GString = exports.GEnum = exports.GPartialMap = exports.GUnionInv = exports.GUnion = exports.GObjectIO = exports.GInterfaceInv = exports.GInterface = exports.GObjectInv = exports.GObject = exports.GInput = exports.GList = exports.NN = void 0;
/**
* ======= PURPOSE =========
* This file contains shortcuts to have a more readable GraphQL typing
*/
const graphql_1 = require("graphql");
const _1 = require(".");
const inversify_1 = require("inversify");
const utils_1 = require("./utils");
const interface_builder_1 = require("./interface-builder");
const object_builder_1 = require("./object-builder");
const inversify_nonnull_1 = require("./inversify-nonnull");
function isPlainGql(t) {
let i = 0;
while (t && t !== Object && t !== Function && t !== Number && t !== Date) {
i++;
if (i > 100) {
throw new Error('Unexpected prototype chain error');
}
if (t === object_builder_1.InversifyObjectTypeBuilderBase) {
return false;
}
if (t === _1.InversifyUnionTypeBuilder) {
return false;
}
t = Object.getPrototypeOf(t);
}
return true;
}
function NN(t) {
return isPlainGql(t)
? new graphql_1.GraphQLNonNull(t)
: inversify_nonnull_1.InversifyNonNull(t);
}
exports.NN = NN;
function GList(t) {
const ttt = _1.InversifyObjectTypeBuilder;
const tt = object_builder_1.InversifyObjectTypeBuilderBase;
return isPlainGql(t)
? new graphql_1.GraphQLList(t)
: _1.InversifyList(t);
}
exports.GList = GList;
/** GraphQL input type */
function GInput(cfg) {
return new graphql_1.GraphQLInputObjectType(cfg);
}
exports.GInput = GInput;
/** GraphQL object */
function GObject(cfg) {
return new graphql_1.GraphQLObjectType(cfg);
}
exports.GObject = GObject;
/** Inversify object shortcut */
function GObjectInv(cfg) {
let QuickInversified = class QuickInversified extends _1.InversifyObjectTypeBuilder {
config() {
return cfg;
}
};
QuickInversified = __decorate([
inversify_1.injectable()
], QuickInversified);
return utils_1.named(QuickInversified, cfg.name);
}
exports.GObjectInv = GObjectInv;
function GInterface(cfg) {
return new graphql_1.GraphQLInterfaceType(cfg);
}
exports.GInterface = GInterface;
/** Inversify interface */
function GInterfaceInv(cfg) {
let QuickInversified = class QuickInversified extends interface_builder_1.InversifyInterfaceTypeBuilder {
config() {
return cfg;
}
};
QuickInversified = __decorate([
inversify_1.injectable()
], QuickInversified);
return utils_1.named(QuickInversified, cfg.name);
}
exports.GInterfaceInv = GInterfaceInv;
function trimResolvers(f) {
if (typeof f === 'function')
return () => trimResolvers(f());
const ret = Object.entries(f)
.map(([k, v]) => {
const nv = { ...v };
delete nv.resolve;
return [k, nv];
});
return Object.fromEntries(ret);
}
/**
* Create both an input & output types, with the same shape.
*
* Usage:
*
* const [MyOutput, MyInput] = GObjectIO({ ...config... })
*
*/
function GObjectIO(cfg) {
return [GObject(cfg), GInput({
...cfg,
name: cfg.name + 'Input',
fields: trimResolvers(cfg.fields),
})];
}
exports.GObjectIO = GObjectIO;
/** GraphQL union */
function GUnion(cfg) {
return new graphql_1.GraphQLUnionType(cfg);
}
exports.GUnion = GUnion;
/** Inversify union */
function GUnionInv(cfg) {
let QuickUnionInversified = class QuickUnionInversified extends _1.InversifyUnionTypeBuilder {
config() {
return cfg;
}
};
QuickUnionInversified = __decorate([
inversify_1.injectable()
], QuickUnionInversified);
return utils_1.named(QuickUnionInversified, cfg.name);
}
exports.GUnionInv = GUnionInv;
/** Inversify partial map shortcut */
function GPartialMap(cfg) {
let PartialMapCtor = class PartialMapCtor extends _1.InversifyPartialMap {
map() {
return cfg;
}
};
PartialMapCtor = __decorate([
inversify_1.injectable()
], PartialMapCtor);
return PartialMapCtor;
}
exports.GPartialMap = GPartialMap;
/** GraphQL enum */
function GEnum(cfg) {
return new graphql_1.GraphQLEnumType(cfg);
}
exports.GEnum = GEnum;
/** GraphQL string */
exports.GString = graphql_1.GraphQLString;
/** GraphQL int */
exports.GInt = graphql_1.GraphQLInt;
/** GraphQL float */
exports.GFloat = graphql_1.GraphQLFloat;
/** GraphQL bool */
exports.GBool = graphql_1.GraphQLBoolean;
// /** GraphQL json */
// export const GJson = GraphQLJSON;
// /** GraphQL date */
// export const GDate = GraphQLDateTime;
//# sourceMappingURL=shortcuts.js.map
;