@giraphql/plugin-errors
Version:
A GiraphQL plugin for adding typed errors into your schema
144 lines • 6.86 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GiraphQLErrorsPlugin = exports.capitalize = void 0;
require("./global-types");
const core_1 = __importStar(require("@giraphql/core"));
__exportStar(require("./types"), exports);
const pluginName = 'errors';
exports.default = pluginName;
function capitalize(s) {
return `${s.slice(0, 1).toUpperCase()}${s.slice(1)}`;
}
exports.capitalize = capitalize;
function createErrorProxy(target) {
return new Proxy(target, {
getPrototypeOf(err) {
const proto = Object.getPrototypeOf(err);
if (!proto) {
return proto;
}
return createErrorProxy(proto);
},
});
}
const errorTypeMap = new WeakMap();
class GiraphQLErrorsPlugin extends core_1.BasePlugin {
onOutputFieldConfig(fieldConfig) {
var _a, _b, _c;
const errorOptions = fieldConfig.giraphqlOptions.errors;
const errorBuilderOptions = this.builder.options.errorOptions;
if (!errorOptions) {
return fieldConfig;
}
const parentTypeName = this.buildCache.getTypeConfig(fieldConfig.parentType).name;
const { types = [], result: { name: resultName = `${parentTypeName}${capitalize(fieldConfig.name)}Success`, fields: resultFieldOptions, ...resultObjectOptions } = {}, union: { name: unionName = `${parentTypeName}${capitalize(fieldConfig.name)}Result`, ...unionOptions } = {}, dataField: { name: dataFieldName = 'data', ...dataField } = {}, } = errorOptions;
const errorTypes = (0, core_1.sortClasses)([
...new Set([...types, ...((_a = errorBuilderOptions === null || errorBuilderOptions === void 0 ? void 0 : errorBuilderOptions.defaultTypes) !== null && _a !== void 0 ? _a : [])]),
]);
const directResult = (_c = (_b = errorOptions.directResult) !== null && _b !== void 0 ? _b : errorBuilderOptions === null || errorBuilderOptions === void 0 ? void 0 : errorBuilderOptions.directResult) !== null && _c !== void 0 ? _c : false;
const typeRef = fieldConfig.type.kind === 'List' ? fieldConfig.type.type.ref : fieldConfig.type.ref;
const typeName = this.builder.configStore.getTypeConfig(typeRef).name;
const unionType = this.runUnique(resultName, () => {
var _a;
let resultType;
if (directResult && !Array.isArray(fieldConfig.giraphqlOptions.type)) {
resultType = fieldConfig.giraphqlOptions.type;
const resultConfig = this.builder.configStore.getTypeConfig(resultType);
if (resultConfig.graphqlKind !== 'Object') {
throw new TypeError(`Field ${parentTypeName}.${fieldConfig.name} must return an ObjectType when 'directResult' is set to true`);
}
}
else {
resultType = this.builder.objectRef(resultName);
resultType.implement({
...resultObjectOptions,
fields: (t) => ({
...resultFieldOptions === null || resultFieldOptions === void 0 ? void 0 : resultFieldOptions(t),
[dataFieldName]: t.field({
...dataField,
type: fieldConfig.giraphqlOptions.type,
nullable: fieldConfig.type.kind === 'List'
? { items: fieldConfig.type.type.nullable, list: false }
: false,
resolve: (data) => data,
}),
}),
});
}
const type = fieldConfig.type.kind === 'List' ? fieldConfig.type.type : fieldConfig.type;
const getDataloader = (_a = this.buildCache.getTypeConfig(type.ref).extensions) === null || _a === void 0 ? void 0 : _a.getDataloader;
return this.builder.unionType(unionName, {
types: [...errorTypes, resultType],
resolveType: (obj) => { var _a; return (_a = errorTypeMap.get(obj)) !== null && _a !== void 0 ? _a : resultType; },
...unionOptions,
extensions: {
...unionOptions.extensions,
getDataloader,
giraphQLPrismaIndirectInclude: {
getType: () => typeName,
path: [{ type: resultName, name: dataFieldName }],
},
},
});
});
return {
...fieldConfig,
extensions: {
...fieldConfig.extensions,
giraphqlErrors: errorTypes,
},
type: {
kind: 'Union',
ref: unionType,
nullable: fieldConfig.type.nullable,
},
};
}
wrapResolve(resolver, fieldConfig) {
var _a;
const giraphqlErrors = (_a = fieldConfig.extensions) === null || _a === void 0 ? void 0 : _a.giraphqlErrors;
if (!giraphqlErrors) {
return resolver;
}
return async (...args) => {
try {
return (await resolver(...args));
}
catch (error) {
for (const errorType of giraphqlErrors) {
if (error instanceof errorType) {
const result = createErrorProxy(error);
errorTypeMap.set(result, errorType);
return result;
}
}
throw error;
}
};
}
}
exports.GiraphQLErrorsPlugin = GiraphQLErrorsPlugin;
core_1.default.registerPlugin(pluginName, GiraphQLErrorsPlugin);
//# sourceMappingURL=index.js.map