graphql-composer
Version:
Create your GraphQL API using composition!
112 lines • 3.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectType = void 0;
const graphql_1 = require("graphql");
const __1 = require("../../../..");
const GQLObjectType_1 = require("./GQLObjectType");
class ObjectType extends GQLObjectType_1.GQLObjectType {
constructor(name) {
super(name);
this._interfaces = [];
}
get interfaces() {
return this._interfaces;
}
get definitionNode() {
return {
kind: "ObjectTypeDefinition",
name: {
kind: "Name",
value: this.name,
},
description: {
kind: "StringValue",
value: this.description,
},
fields: this._fields.map((f) => f.definitionNode),
directives: this.directives.map((d) => d.definitionNode),
};
}
static create(nameOrType) {
if (typeof nameOrType === "string") {
return new ObjectType(nameOrType);
}
else if (nameOrType instanceof __1.GQLType) {
const obj = ObjectType.create(nameOrType.name)
.setExtensions(nameOrType.extensions)
.setDirectives(...nameOrType.directives)
.setDescription(nameOrType.description);
if (nameOrType instanceof __1.InputType) {
obj.setFields(...nameOrType.fields.map((f) => __1.Field.create(f)));
}
else {
const objType = nameOrType;
obj.setFields(...objType.fields);
if (objType instanceof ObjectType) {
obj.setInterfaces(...objType.interfaces);
}
}
return obj;
}
else {
const t = ObjectType.create(nameOrType.name);
t._classType = nameOrType;
return t;
}
}
build() {
this._fields = [
...this._fields,
...this._interfaces.flatMap((i) => i.fields),
];
const built = new graphql_1.GraphQLObjectType({
name: this._name,
description: this.description,
fields: () => this.getFields(),
interfaces: () => this.interfaces.map((i) => i.built),
});
this._built = built;
return built;
}
/**
* The interfaces to implement
* @param interfaces
*/
setInterfaces(...interfaces) {
this._interfaces = interfaces;
this._interfaces.map((i) => {
i.addPossibleTypes(this);
});
return this;
}
/**
* Add some interfaces to implement
* @param interfaces the interfaces to implements
*/
addInterfaces(...interfaces) {
return this.setInterfaces(...this._interfaces, ...interfaces);
}
/**
* Remove some interfaces from implementation
* @param interfaces the interfaces ID's to remove
*/
removeInterfaces(...interfaces) {
this._interfaces = __1.ArrayHelper.remove(interfaces, this._interfaces);
return this;
}
/**
* Add a suffix to the name of your type ("ObjectType" by default)
* @param suffix The suffix to add to the name
*/
suffix(suffix = "Object") {
return this.setName(this.name + suffix);
}
copy() {
return ObjectType.create(this);
}
convert(to) {
return to.create(this);
}
}
exports.ObjectType = ObjectType;
//# sourceMappingURL=ObjectType.js.map