@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
223 lines (216 loc) • 7.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GraphType = void 0;
exports.createType = createType;
exports.getType = getType;
var _utils = require("@backland/utils");
var _CircularDeps = require("../CircularDeps");
var _ObjectType = require("../ObjectType");
var _extendObjectDefinition = require("../extendObjectDefinition");
var _extendType = require("../extendType");
var _initGraphType = require("./initGraphType");
class GraphType {
static __isGraphType = true;
__isGraphType = true;
static register = (0, _utils.createStore)();
static resolvers = (0, _utils.createStore)();
static reset = async () => {
this.resolvers.clear();
this.register.clear();
};
get id() {
if (this.optionalId) return this.optionalId;
throw new _utils.RuntimeError(['The method you are trying to execute needs the graphType used to be identified.' + ' Use ``myType.identify("name")`` and try again.', 'You can also read that information from ``myType.optionalId.``', '', ''].join('\n'), this.__lazyGetter.definitionInput);
}
_optionalId = undefined;
get optionalId() {
return this._optionalId;
}
constructor(...args) {
(0, _initGraphType.initGraphType)(this, args);
}
// used to lazy process input definition to improve circular dependency in types
touched = false;
touch() {
// just dispatch lazy loader getters
this.__lazyGetter.id;
return this;
}
__hidden = false;
identify = name => {
this._optionalId = name;
if (GraphType.register.has(name)) {
//
} else {
if (!(0, _utils.isBrowser)()) {
var _CircularDeps$typesWr;
(_CircularDeps$typesWr = _CircularDeps.CircularDeps.typesWriter) === null || _CircularDeps$typesWr === void 0 ? void 0 : _CircularDeps$typesWr.BacklandWatchTypesPubSub.emit('created', {
graphType: this
});
}
GraphType.register.set(name, this);
}
};
set hidden(value) {
this.__lazyGetter.field.hidden = value;
this.__hidden = value;
}
get hidden() {
return this.__hidden;
}
parse = (input, options) => {
const field = this.__lazyGetter.field;
try {
const _options = typeof options === 'object' ? options : {};
const {
includeHidden = true
} = _options;
if (this.__hidden && !includeHidden) return undefined;
return field.parse(input, _options);
} catch (e) {
let message = e.message;
if (field.list) {
message = `➤ ${message}`;
} else {
message = `➤ ${this.optionalId || ''} ${e.message}`;
}
e.message = message;
throw e;
}
};
_toGraphQL = () => {
// @ts-ignore
return _CircularDeps.CircularDeps.GraphQLParser.fieldToGraphQL({
field: this.__lazyGetter.field,
fieldName: this.id,
parentName: this.id,
path: [`Type_${this.id}`]
});
};
graphQLType = (...args) => {
return this._toGraphQL().type(...args);
};
graphQLInputType = (...args) => {
return this._toGraphQL().inputType(...args);
};
graphQLInterface = (...args) => {
if (!this.__lazyGetter.objectType) {
throw new Error('graphQLInterface is only available for object type');
}
// @ts-ignore
return _CircularDeps.CircularDeps.GraphQLParser.objectToGraphQL({
object: this.__lazyGetter.objectType
}).interfaceType(...args);
};
clone(handler) {
const parsed = (0, _ObjectType.parseField)(this.definition);
const input = (0, _extendObjectDefinition.extendObjectDefinition)(parsed);
return handler(input);
}
override(handler) {
const input = (0, _extendType.extendType)(this.definition);
return handler(input);
}
beforeInitialize = [];
mutateFields(callback) {
if (this.touched) {
throw new Error(`Called "mutateFields" after type "${this.optionalId || ''}" was touched.`);
}
if (this.optionalId) {
// ObjectType.register.remove(this.optionalId); // FIXME
}
this.beforeInitialize.push(payload => {
if (payload.definition.type !== 'object') {
throw new Error(`mutateFields can only be used with object types.`);
}
try {
const input = (0, _extendObjectDefinition.extendObjectDefinition)(payload.definition);
payload.definition.def = callback(input);
payload.objectType = (0, _ObjectType.createObjectType)({
[this.id]: this.definition
});
payload.field.utils.object = payload.objectType;
return payload;
} catch (e) {
e.message = `Failed to execute mutateFields with the result from callback: ${(0, _utils.inspectObject)({
callback
})}`;
throw e;
}
});
return this;
}
print = () => {
const type = this.graphQLType();
const inputType = this.graphQLInputType();
// @ts-ignore circular
const {
GraphQLSchema,
printSchema
} = _CircularDeps.CircularDeps.graphql;
const object = new GraphQLSchema({
// @ts-ignore
types: [type, inputType]
});
return printSchema(object).split('\n');
};
typescriptPrint = options => {
const name = (options === null || options === void 0 ? void 0 : options.name) || this.id;
// @ts-ignore
const object = this.__lazyGetter.objectType || (0, _ObjectType.createObjectType)({
[name]: this.definition
});
// @ts-ignore circular
return _CircularDeps.CircularDeps.objectToTypescript(name, object, options);
};
optionalType = name => {
let _id = name || this.optionalId;
_id = _id ? `${_id}Optional` : undefined;
return this.override(it => it.optional()).graphType(_id);
};
requiredType = name => {
let _id = name || this.optionalId;
_id = _id ? `${_id}NotNull` : undefined;
return this.override(it => it.required()).graphType(_id);
};
listType = name => {
let _id = name || this.optionalId;
_id = _id ? `${_id}List` : undefined;
return this.override(it => it.list()).graphType(_id);
};
singleType = name => {
let _id = name || this.optionalId;
_id = _id ? `${_id}Item` : undefined;
return this.override(it => it.single()).graphType(_id);
};
/**
* Get an Object with the provided id
* or set a new Object in the register if not found.
* @param id
* @param def
*/
static getOrSet = (id, def) => {
const existing = GraphType.register.has(id) && GraphType.register.get(id);
if (existing) return existing;
return new GraphType(id, def);
};
static is(input) {
return (input === null || input === void 0 ? void 0 : input.__isGraphType) === true;
}
static isTypeDefinition(input) {
var _input$type;
return (input === null || input === void 0 ? void 0 : (_input$type = input.type) === null || _input$type === void 0 ? void 0 : _input$type.__isGraphType) === true;
}
}
exports.GraphType = GraphType;
function createType(...args) {
return new GraphType(
// @ts-ignore
...args);
}
function getType(name) {
return GraphType.register.get(name);
}
//# sourceMappingURL=GraphType.js.map