@pothos/core
Version:
Pothos (formerly GiraphQL) is a plugin based schema builder for creating code-first GraphQL schemas in typescript
85 lines (84 loc) • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "TypeRefWithFields", {
enumerable: true,
get: function() {
return TypeRefWithFields;
}
});
const _base = require("./base");
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class TypeRefWithFields extends _base.BaseTypeRef {
addFields(fields) {
for (const cb of this.fieldCbs){
for (const [name, ref] of Object.entries(fields())){
if (ref) {
cb(name, ref);
}
}
}
this.fields.add(fields);
}
addInterfaces(interfaces) {
if (Array.isArray(interfaces) && interfaces.length === 0) {
return;
}
if (this.preparedForBuild) {
this.updateConfig((cfg)=>({
...cfg,
interfaces: [
...cfg.interfaces,
...typeof interfaces === 'function' ? interfaces() : interfaces
]
}));
} else {
this.interfaces.push(()=>Array.isArray(interfaces) ? interfaces : interfaces());
}
}
onField(cb) {
this.fieldCbs.add(cb);
for (const fieldMap of this.fields){
for (const [name, ref] of Object.entries(fieldMap())){
if (ref) {
cb(name, ref);
}
}
}
}
prepareForBuild() {
if (this.preparedForBuild) {
return;
}
super.prepareForBuild();
if (this.interfaces.length > 0) {
this.updateConfig((cfg)=>{
var _cfg_interfaces;
return {
...cfg,
interfaces: [
...(_cfg_interfaces = cfg.interfaces) !== null && _cfg_interfaces !== void 0 ? _cfg_interfaces : [],
...this.interfaces.flatMap((interfaces)=>interfaces())
]
};
});
}
}
constructor(...args){
super(...args), _define_property(this, "fields", new Set()), _define_property(this, "fieldCbs", new Set()), _define_property(this, "interfaces", []);
}
}
//# sourceMappingURL=base-with-fields.js.map