@pothos/plugin-scope-auth
Version:
A Pothos plugin for adding scope based authorization checks to your GraphQL Schema
26 lines (25 loc) • 1.02 kB
JavaScript
import SchemaBuilder, { isThenable } from '@pothos/core';
import { ForbiddenError } from './errors.js';
import RequestCache from './request-cache.js';
const schemaBuilderProto = SchemaBuilder.prototype;
schemaBuilderProto.runAuthScopes = function runAuthScopes(context, scopes, unauthorizedError = (result) => new ForbiddenError(result.message, result.failure)) {
const cache = RequestCache.fromContext(context, this);
const resultOrPromise = cache.evaluateScopeMap(scopes);
if (isThenable(resultOrPromise)) {
return resultOrPromise.then(handleScopeResult);
}
handleScopeResult(resultOrPromise);
function handleScopeResult(result) {
if (result) {
const error = unauthorizedError({
message: "Unauthorized",
failure: result
});
if (typeof error === "string") {
throw new ForbiddenError(error, result);
}
throw error;
}
}
};
//# sourceMappingURL=schema-builder.js.map