@deep-foundation/deeplinks
Version:
[](https://www.npmjs.com/package/@deep-foundation/deeplinks) [](https://gitpod.io/#https://github.com/deep-fo
211 lines • 8.04 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import Debug from 'debug';
import gql from 'graphql-tag';
import { _serialize } from '../client.js';
const debug = Debug('deeplinks:gql:query');
const log = debug.extend('log');
const error = debug.extend('error');
const fieldsInputs = (tableName) => ({
'distinct_on': `[${tableName}_select_column!]`,
'limit': `Int`,
'offset': `Int`,
'order_by': `[${tableName}_order_by!]`,
'where': `${tableName}_bool_exp!`,
});
const manyRelations = {
links: {
'from': false,
'to': false,
'type': false,
'in': true,
'out': true,
'typed': true,
selected: true,
selectors: true,
can_rule: true,
can_action: true,
can_object: true,
can_subject: true,
down: true,
up: true,
tree: true,
root: true,
},
selector: {
item: false,
selector: false,
query: false,
},
can: {
rule: false,
action: false,
object: false,
subject: false,
},
tree: {
link: false,
tree: false,
root: false,
parent: false,
by_link: true,
by_tree: true,
by_root: true,
by_parent: true,
by_position: true,
},
value: {
link: false,
},
string: {
link: false,
},
number: {
link: false,
},
object: {
link: false,
},
files: {
link: false,
},
handlers: {
link: false,
dist: false,
execution_provider: false,
handler: false,
isolation_provider: false,
src: false,
},
};
export const generateQueryData = ({ tableName, tableNamePostfix = '', operation = 'query', queryName = `${tableName}`, returning = `id`, variables: _variables, }) => {
log('generateQuery', { tableName, tableNamePostfix, operation, queryName, returning, _variables });
const fields = ['distinct_on', 'limit', 'offset', 'order_by', 'where'];
const _returning = typeof (returning) === 'string' ? (tableName) => returning : returning;
return (alias, index) => {
const _a = _variables.where, { return: customReturn } = _a, where = __rest(_a, ["return"]), __variables = __rest(_variables, ["where"]);
const variables = Object.assign(Object.assign({}, __variables), { where });
log('generateQueryBuilder', { tableName, tableNamePostfix, operation, queryName, returning, variables, alias, index });
const generateDefs = (fields, index, tableName, postfix = '') => {
const fieldTypes = fieldsInputs(tableName);
const defs = [];
const args = [];
for (let f = 0; f < fields.length; f++) {
const field = fields[f];
const key = field + index + postfix;
defs.push(`$${key}: ${fieldTypes[field]}`);
args.push(`${field}: $${key}`);
}
return { defs, args };
};
const { defs, args } = generateDefs(fields, index, tableName);
const resultAlias = `${alias}${typeof (index) === 'number' ? index : ''}`;
const resultVariables = {};
for (const v in variables) {
if (Object.prototype.hasOwnProperty.call(variables, v)) {
const variable = variables[v];
resultVariables[v + index] = variable;
}
}
let customReturnAliases = ``;
const generateCustomArgsAndVariables = (customReturn, prefix = '', tableName) => {
var _a, _b;
let result = '';
for (let r in customReturn) {
const _c = customReturn[r], { return: _return, relation, table, distinct_on, limit, offset, order_by } = _c, customWhere = __rest(_c, ["return", "relation", "table", "distinct_on", "limit", "offset", "order_by"]);
const _table = (_a = _serialize[tableName]) === null || _a === void 0 ? void 0 : _a.relations[relation];
if (!_table)
throw new Error(`relation ${relation} not found in table ${tableName}`);
const postfix = `${prefix}_${r}`;
let customReturning = '';
if (_return) {
customReturning += generateCustomArgsAndVariables(_return, postfix, _table);
}
if ((_b = manyRelations === null || manyRelations === void 0 ? void 0 : manyRelations[tableName]) === null || _b === void 0 ? void 0 : _b[relation]) {
const { defs: _defs, args: _args } = generateDefs(fields, index, _table, postfix);
defs.push(..._defs);
const variable = customWhere;
resultVariables['where' + index + postfix] = variable;
result += ` ${r}: ${customReturn[r].relation}(${_args.join(',')}) { ${_serialize[_table].returning} ${customReturning} }`;
if (distinct_on) {
resultVariables['distinct_on' + index + postfix] = distinct_on;
}
if (limit) {
resultVariables['limit' + index + postfix] = limit;
}
if (offset) {
resultVariables['offset' + index + postfix] = offset;
}
if (order_by) {
resultVariables['order_by' + index + postfix] = order_by;
}
}
else {
result += ` ${r}: ${customReturn[r].relation} { ${_serialize[_table].returning} ${customReturning} }`;
}
}
return result;
};
customReturnAliases += generateCustomArgsAndVariables(customReturn, '', tableName);
const result = {
tableName,
tableNamePostfix,
operation,
queryName: queryName + tableNamePostfix,
returning,
variables,
resultReturning: `${_returning(tableName)}${customReturnAliases || ''}`,
fields,
index,
defs,
args,
alias,
resultAlias,
resultVariables,
};
log('generateQueryResult', result);
return result;
};
};
;
;
export const generateQuery = ({ queries = [], operation = 'query', name = 'QUERY', alias = 'q', }) => {
log('generateQuery', { name, alias, queries });
const calledQueries = queries.map((m, i) => typeof (m) === 'function' ? m(alias, i) : m);
const defs = calledQueries.map(m => m.defs.join(',')).join(',');
const queryString = `${operation} ${name} (${defs}) { ${calledQueries.map(m => `${m.resultAlias}: ${m.queryName}(${m.args.join(',')}) { ${m.resultReturning} }`).join('')} }`;
let query;
try {
query = gql `${queryString}`;
}
catch (e) {
throw e;
}
const variables = {};
for (let a = 0; a < calledQueries.length; a++) {
const action = calledQueries[a];
for (const v in action.resultVariables) {
if (Object.prototype.hasOwnProperty.call(action.resultVariables, v)) {
const variable = action.resultVariables[v];
variables[v] = variable;
}
}
}
const result = {
query,
variables,
queryString,
};
log('generateQueryResult', JSON.stringify({ query: queryString, variables }, null, 2));
return result;
};
//# sourceMappingURL=query.js.map