UNPKG

graphql-gene

Version:

Generates automatically an executable schema out of your ORM models

180 lines (177 loc) 5.21 kB
import { buildSchema as d, buildASTSchema as b, GraphQLSchema as D, extendSchema as E, parse as v, isSpecifiedDirective as x, isSpecifiedScalarType as A, parseType as F, GraphQLNonNull as j, GraphQLList as p, isScalarType as O, print as G } from "graphql"; function y() { return globalThis.__graphqlGeneExtendedTypes = globalThis.__graphqlGeneExtendedTypes ?? { config: {}, geneConfig: {} }, globalThis.__graphqlGeneExtendedTypes; } function w(e, t) { if (!t) return; const r = y(); r.geneConfig[e] = t; } function L(e) { const t = y(); Object.entries(e).forEach(([r, i]) => { if (!T(i)) throw new Error(`Provided field config for type "${r}" must be an object.`); const n = r; t.config[n] = t.config[n] || {}, t.config[n] = { ...t.config[n], ...i }; }); } function Q(e, t) { if (!e && (!t || !t.length)) return; let r; if (e && (r = typeof e == "string" ? d(e) : e instanceof D ? e : b(e)), t?.length) { const n = t.filter((o) => !r?.getType(o)).map((o) => `scalar ${o}`).join(` `); r = r ? E(r, v(n)) : d(n); } return r; } function $(e) { const t = e.schema.getQueryType(), r = e.schema.getMutationType(); if (!t && !r) return; const i = /* @__PURE__ */ new Set([]), n = (o, c) => { if (i.has(o.name)) return; i.add(o.name); const f = o.getFields(); Object.entries(f).forEach(([m, a]) => { const s = a, l = a.type; if (!s || !l) return; const u = g(l); if (!u) return; const S = e.each({ field: m, fieldDef: s, get isList() { return s instanceof p || "ofType" in l && l.ofType instanceof p; }, get isNonNullable() { return s instanceof j; }, parentType: o.name, parentTypeDef: o, state: c, type: u.name, get typeDef() { return e.schema.getType(u.name); } }); "getFields" in u && n(u, S || c); }); }; t && n(t, e.state || {}), r && n(r, e.state || {}); } function g(e) { const t = "ofType" in e ? g(e.ofType) : e; if (t && "name" in t) return t; } function M(e) { const t = Array.isArray(e), r = t ? typeof e[0] : typeof e, i = { string: "String", number: "Int", boolean: "Boolean" }; return t ? `[${i[r]}!]` : i[r]; } function q(e) { let t = ""; const r = (n) => { O(n) && (t += `scalar ${n.name} `), n && "astNode" in n && n.astNode && (t += `${G(n.astNode)} `); }; e.getDirectives().forEach((n) => { x(n) || r(n); }); const i = Object.entries(e.getTypeMap()); return i.sort(([n], [o]) => n === "Query" ? -1 : n === "Mutation" ? o === "Query" ? 1 : -1 : o === "Query" ? 1 : o === "Mutation" ? n === "Query" ? -1 : 1 : n.localeCompare(o)), i.filter(([n]) => !/^__/.test(n)).forEach(([, n]) => { A(n) || r(n); }), t.replace(/\n$/, ""); } function N(e) { return ["resolver", "args"].some( (t) => t in e && e[t] === "default" ); } function k() { return { varType: "type", directives: /* @__PURE__ */ new Set([]), lines: {} }; } function R() { return { directives: /* @__PURE__ */ new Set([]), typeDef: "", argsDef: {} }; } function T(e) { return e !== null && typeof e == "object"; } function I(e) { for (const t in e) return !1; return !0; } function B(e) { return Array.isArray(e); } function W(e) { return T(e); } function z(e) { return typeof e == "string" ? { returnType: e } : e; } function P(e) { return (e.kind === "NonNullType" ? e.type : e).kind === "ListType"; } function h(e) { return "type" in e ? h(e.type) : e.name.value; } function U(e) { const t = e.schema.getType(e.parent); if (!t) return; const i = ("getFields" in t ? t.getFields() : void 0)?.[e.field]; return i && "resolve" in i ? i : void 0; } function H(e) { return h(F(e)); } function J(e) { return e.geneConfig || e.model && (typeof e.model == "object" || typeof e.model == "function") && "geneConfig" in e.model && e.model.geneConfig || void 0; } function V(e, t) { const r = e || {}, i = (c) => { for (const f of c) if (typeof f == "string" && f === t || f instanceof RegExp && f.test(t)) return !0; }; if (r.include && !i(r.include)) return !1; let n = /* @__PURE__ */ new Set(["createdAt", "updatedAt"]); Array.isArray(r.includeTimestamps) ? r.includeTimestamps.forEach((c) => n.delete(c)) : r.includeTimestamps === !0 && (n = /* @__PURE__ */ new Set([])); const o = [...r.exclude || [], ...n]; return !i(o); } function X(e, t, r) { e[r] = { ...k(), ...e[r] }, e[r].varType = t; } export { X as createTypeDefLines, L as extendTypes, g as findAccurateTypeDef, h as findTypeNameFromTypeNode, R as getDefaultFieldLinesObject, k as getDefaultTypeDefLinesObject, U as getFieldDefinition, J as getGeneConfigFromOptions, y as getGloballyExtendedTypes, M as getGraphqlType, H as getReturnTypeName, B as isArrayFieldConfig, I as isEmptyObject, V as isFieldIncluded, P as isListType, T as isObject, W as isObjectFieldConfig, N as isUsingDefaultResolver, $ as lookDeepInSchema, z as normalizeFieldConfig, Q as parseSchemaOption, q as printSchemaWithDirectives, w as setGeneConfigByType }; //# sourceMappingURL=index.js.map