UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

118 lines (116 loc) 5.04 kB
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import { RuntimeError } from '@backland/utils'; import get from 'lodash/get'; import setWith from 'lodash/setWith'; import { objectToQuery } from './objectToQuery'; var utilsKey = '$'; var argsKey = '$A'; var functionKey = '$F'; var sepKey = '.'; var specialKeys = new Set([utilsKey, argsKey, functionKey, sepKey, sepKey]); // Converts a JavaScript object to a GraphQL Query string // Full support for nested query / mutation nodes and arguments // Optionally strip specific object keys using the ignoreFields option // Support for input arguments [OK] // Support for query aliases via __aliasFor // Support for Enum values via EnumType // Support for variables via __variables // Support for simple directives (such as @client) via __directives // Support for one or more inline fragments via __on.__typeName // Support for full fragments via __all_on // Support for named queries/mutations via __name export class QueryBuilder { constructor(builder) { var _this2 = this; _defineProperty(this, "chain", new Set()); _defineProperty(this, "edges", new Map()); _defineProperty(this, "object", {}); _defineProperty(this, "query", ''); _defineProperty(this, "builder", void 0); _defineProperty(this, "read", () => { this.object = Object.create(null); var self = this; var chainArray = [...self.chain.values()]; chainArray.forEach(entry => { var isFunction = entry.indexOf(utilsKey) > -1; var hasArgs = entry.endsWith(argsKey); if (!isFunction) { var node = Object.create(null); setWith(this.object, entry, node); } else { var _method; var parts = entry.split(/(\.|\$F|\$A)/).filter(item => item && !specialKeys.has(item)); var _args = hasArgs ? parts.pop() : undefined; var method = hasArgs ? parts.pop() : undefined; if (method === '$allOn') method = '$all_on'; var isUtil = (_method = method) === null || _method === void 0 ? void 0 : _method.startsWith(utilsKey); if (isUtil && method) { method = "__".concat(method.replace(utilsKey, '')); } var newEntry = method ? [...parts, method] : parts; if (!isUtil && hasArgs) { newEntry.push('__args'); } var newKey = newEntry.filter(el => el.indexOf('{') === -1 && el.indexOf(utilsKey) === -1).join(sepKey); try { var json = _args ? JSON.parse(_args) : get(this.object, newKey, {}); setWith(this.object, newKey, json); } catch (e) { throw new RuntimeError("failed to convert args", { args: _args, entry }); } } }); return this.query = objectToQuery(this.object, { includeFalsyKeys: true, pretty: true }); }); _defineProperty(this, "build", function () { var parent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var self = _this2; var parentName = parent.join('.'); function realItem() {} Object.defineProperty(realItem, 'name', { value: parentName }); self.edges.set(parentName, realItem); return new Proxy(realItem, { apply: function apply(_, _this, args) { var propName = parent.pop(); var next = "$F".concat(propName, "$F"); var argString = ![undefined, ''].includes(args[0]) ? "$A".concat(JSON.stringify(args[0]), "$A") : ''; var newNext = [...parent, "".concat(next).concat(argString)]; self.chain.add(newNext.join('.')); return self.build(newNext); }, get(_, prop) { if (prop.startsWith(utilsKey)) { // prop = '__REMOVE__'; } if (prop === '0') { return self.build(parent); } var next = [...parent, prop]; var field = next.join('.'); self.chain.add(field); return self.build(next); }, set(target, p, value) { target[p] = value; return true; } }); }); this.builder = builder; builder(this.build()); } } export function graphGet(builder) { return new QueryBuilder(builder); } //# sourceMappingURL=graphGet.js.map