graphql-compose-mongoose
Version:
Plugin for `graphql-compose` which derive a graphql types from a mongoose model.
54 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuntimeError = exports.ValidationError = void 0;
exports.getErrorInterface = getErrorInterface;
const graphql_compose_1 = require("graphql-compose");
const MongoError_1 = require("./MongoError");
const ValidationError_1 = require("./ValidationError");
Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return ValidationError_1.ValidationError; } });
const RuntimeError_1 = require("./RuntimeError");
Object.defineProperty(exports, "RuntimeError", { enumerable: true, get: function () { return RuntimeError_1.RuntimeError; } });
function getErrorInterface(schemaComposer) {
const ErrorInterface = schemaComposer.getOrCreateIFTC('ErrorInterface', (iftc) => {
iftc.addFields({
message: {
description: 'Generic error message',
type: 'String',
},
});
const ValidationErrorOTC = (0, ValidationError_1.getValidationErrorOTC)(schemaComposer);
const MongoErrorOTC = (0, MongoError_1.getMongoErrorOTC)(schemaComposer);
const RuntimeErrorOTC = (0, RuntimeError_1.getRuntimeErrorOTC)(schemaComposer);
ValidationErrorOTC.addInterface(iftc);
MongoErrorOTC.addInterface(iftc);
RuntimeErrorOTC.addInterface(iftc);
schemaComposer.addSchemaMustHaveType(ValidationErrorOTC);
schemaComposer.addSchemaMustHaveType(MongoErrorOTC);
schemaComposer.addSchemaMustHaveType(RuntimeErrorOTC);
let ValidationErrorType;
let MongoErrorType;
let RuntimeErrorType;
if (graphql_compose_1.graphqlVersion >= 16) {
ValidationErrorType = ValidationErrorOTC.getTypeName();
MongoErrorType = MongoErrorOTC.getTypeName();
RuntimeErrorType = RuntimeErrorOTC.getTypeName();
}
else {
ValidationErrorType = ValidationErrorOTC.getType();
MongoErrorType = MongoErrorOTC.getType();
RuntimeErrorType = RuntimeErrorOTC.getType();
}
iftc.setResolveType((value) => {
switch (value === null || value === void 0 ? void 0 : value.name) {
case 'ValidationError':
return ValidationErrorType;
case 'MongoError':
return MongoErrorType;
default:
return RuntimeErrorType;
}
});
});
return ErrorInterface;
}
//# sourceMappingURL=index.js.map