UNPKG

fuse

Version:

The magical GraphQL framework

325 lines (319 loc) 9.98 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/builder.ts var builder_exports = {}; __export(builder_exports, { AuthenticationError: () => AuthenticationError, BadRequestError: () => BadRequestError, ForbiddenError: () => ForbiddenError, FuseError: () => FuseError, NotFoundError: () => NotFoundError, addMutationFields: () => addMutationFields, addNodeFields: () => addNodeFields, addObjectFields: () => addObjectFields, addQueryFields: () => addQueryFields, builder: () => reducedBuilder, decodeGlobalID: () => import_plugin_relay2.decodeGlobalID, defineAuthScopes: () => defineAuthScopes, encodeGlobalID: () => import_plugin_relay2.encodeGlobalID, enumType: () => enumType, inputType: () => inputType, interfaceType: () => interfaceType, node: () => node, objectType: () => objectType, unionType: () => unionType }); module.exports = __toCommonJS(builder_exports); var import_core3 = __toESM(require("@pothos/core")); var import_plugin_relay = __toESM(require("@pothos/plugin-relay")); var import_plugin_scope_auth = __toESM(require("@pothos/plugin-scope-auth")); var import_plugin_dataloader = __toESM(require("@pothos/plugin-dataloader")); var import_graphql_scalars = require("graphql-scalars"); // src/pothos-list/schema-builder.ts var import_core = __toESM(require("@pothos/core")); var schemaBuilderProto = import_core.default.prototype; var listRefs = /* @__PURE__ */ new WeakMap(); var globalListFieldsMap = /* @__PURE__ */ new WeakMap(); schemaBuilderProto.listObject = function listObject({ type, name: listName, nullable }) { (0, import_core.verifyRef)(type); const listRef = this.objectRef(listName); this.objectType(listRef, { fields: (t) => ({ totalCount: t.int({ nullable: true, resolve: (parent) => parent.totalCount || null }), nodes: t.field({ nullable: { items: nullable ?? true, list: false }, type: [type], resolve: (parent) => parent.nodes }) }) }); if (!listRefs.has(this)) { listRefs.set(this, []); } listRefs.get(this).push(listRef); globalListFieldsMap.get(this)?.forEach((fieldFn) => void fieldFn(listRef)); return listRef; }; // src/pothos-list/index.ts var import_core2 = __toESM(require("@pothos/core")); var pluginName = "fuselist"; var pothos_list_default = pluginName; var PothosListPlugin = class extends import_core2.BasePlugin { }; try { import_core2.default.registerPlugin(pluginName, PothosListPlugin); } catch (e) { } var fieldBuilderProto = import_core2.RootFieldBuilder.prototype; fieldBuilderProto.list = function list(fieldOptions) { const ref = this.builder.objectRef( "Unnamed list" ); const fieldRef = this.field({ ...fieldOptions, type: ref, args: { ...fieldOptions.args }, resolve: fieldOptions.resolve }); this.builder.configStore.onFieldUse(fieldRef, (fieldConfig) => { const name = fieldConfig.name[0].toUpperCase() + fieldConfig.name.slice(1); const listName = `${this.typename}${name}${fieldConfig.name.toLowerCase().endsWith("list") ? "" : "List"}`; this.builder.listObject({ type: fieldOptions.type, name: listName, nullable: fieldOptions.nodeNullable ?? true }); this.builder.configStore.associateRefWithName(ref, listName); }); return fieldRef; }; // src/errors.ts var import_graphql = require("graphql"); var FuseError = class extends import_graphql.GraphQLError { constructor(message, extensions) { super(message, { extensions }); } }; var AuthenticationError = class extends FuseError { constructor(message = "Unauthenticated") { super(message, { code: "UNAUTHENTICATED" }); this.name = "UnauthenticatedError"; } }; var ForbiddenError = class extends FuseError { constructor(message = "Forbidden") { super(message, { code: "FORBIDDEN" }); this.name = "ForbiddenError"; } }; var NotFoundError = class extends FuseError { constructor(message = "Not Found") { super(message, { code: "NOT_FOUND" }); this.name = "NotFoundError"; } }; var BadRequestError = class extends FuseError { constructor(message = "Bad Request") { super(message, { code: "BAD_REQUEST" }); this.name = "BadRequestError"; } }; // src/builder.ts var import_plugin_relay2 = require("@pothos/plugin-relay"); function throwFirstError(failure) { if ("error" in failure && failure.error) { throw failure.error; } if (failure.kind === import_plugin_scope_auth.AuthScopeFailureType.AnyAuthScopes || failure.kind === import_plugin_scope_auth.AuthScopeFailureType.AllAuthScopes) { for (const child of failure.failures) { throwFirstError(child); } } } var builder = new import_core3.default({ plugins: [import_plugin_relay.default, import_plugin_scope_auth.default, import_plugin_dataloader.default, pothos_list_default], defaultFieldNullability: true, authScopes: async (context) => { return await scopesFunc(context); }, scopeAuthOptions: { runScopesOnType: true, treatErrorsAsUnauthorized: true, unauthorizedError: (_, __, ___, result) => { throwFirstError(result.failure); throw new ForbiddenError("Not authorized"); } }, relayOptions: { clientMutationId: "omit", cursorType: "String" } }); builder.queryType({ fields: (t) => ({ _version: t.string({ nullable: false, resolve: () => "0.12.0" }) }) }); builder.mutationType({ fields: (t) => ({ _version: t.string({ nullable: false, resolve: () => "0.12.0" }) }) }); builder.addScalarType("JSON", import_graphql_scalars.JSONResolver, {}); builder.addScalarType("Date", import_graphql_scalars.DateResolver, {}); var reducedBuilder = builder; var scopesFunc = (ctx) => { }; var hasCalledDefineAuthScopes = false; var defineAuthScopes = (func) => { if (hasCalledDefineAuthScopes) { console.warn( "You can only call defineAuthScopes once, all but the first call have been ignored." ); } else { hasCalledDefineAuthScopes = true; scopesFunc = func; } }; function node(opts) { const node2 = builder.loadableNode(opts.name, { description: opts.description, isTypeOf: opts.isTypeOf, fields: opts.fields, interfaces: opts.interfaces, authScopes: opts.authScopes, id: { resolve: (parent) => { const key = parent[opts.key || "id"]; if (!key) { throw new Error( "Could not find key for node, did you forget to set the 'key' option?" ); } return key; } }, async load(ids, ctx) { const translatedIds = ids.map((id) => { try { if (typeof id !== "string") return id; const decoded = (0, import_plugin_relay.decodeGlobalID)(id); return decoded.id; } catch (e) { return id; } }); const results = await opts.load(translatedIds, ctx); return results.map( (result) => result instanceof Error ? result : { ...result, __typename: opts.name } ); } }); builder.queryField( "" + opts.name[0].toLowerCase() + opts.name.slice(1), (t) => t.field({ type: node2, args: { id: t.arg.id({ required: true }) }, // @ts-expect-error resolve: (parent, args) => { return args.id; } }) ); return node2; } function objectType(opts) { const { name, ...options } = opts; return builder.objectRef(name).implement(options); } var addQueryFields = builder.queryFields.bind(builder); var addMutationFields = builder.mutationFields.bind(builder); var addObjectFields = builder.objectFields.bind(builder); var addNodeFields = builder.objectFields.bind(builder); var enumType = (opts) => { const { name, ...options } = opts; return builder.enumType(name, options); }; var inputType = (opts) => { const { name, ...options } = opts; return builder.inputType(name, options); }; var interfaceType = (opts) => { const { name, ...options } = opts; return builder.interfaceRef(name).implement(options); }; var unionType = (opts) => { const { name, ...options } = opts; return builder.unionType(name, options); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AuthenticationError, BadRequestError, ForbiddenError, FuseError, NotFoundError, addMutationFields, addNodeFields, addObjectFields, addQueryFields, builder, decodeGlobalID, defineAuthScopes, encodeGlobalID, enumType, inputType, interfaceType, node, objectType, unionType });