join-monster
Version:
A GraphQL to SQL query execution layer for batch data fetching.
116 lines (115 loc) • 3.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = defineObjectShape;
var _util = require("./util");
var _aliases = require("./aliases");
function defineObjectShape(topNode) {
(0, _util.validateSqlAST)(topNode);
return _defineObjectShape(null, '', topNode);
}
function _defineObjectShape(parent, prefix, node) {
var _ref;
const prefixToPass = parent ? prefix + node.as + '__' : prefix;
const fieldDefinition = {};
for (let child of node.children) {
const setField = definition => {
const originalKey = child.fieldName;
if ((0, _aliases.hasConflictingSiblings)(child, node.children) && !child.sqlBatch) {
const aliasKey = (0, _aliases.getAliasKey)(originalKey, child.alias);
fieldDefinition[aliasKey] = definition;
fieldDefinition[originalKey] = {
column: '__jm__' + aliasKey,
type: () => _aliases.resolveAliasValue
};
} else {
fieldDefinition[originalKey] = definition;
}
};
switch (child.type) {
case 'column':
setField(prefixToPass + child.as);
break;
case 'composite':
setField(prefixToPass + child.as);
break;
case 'columnDeps':
for (let name in child.names) {
fieldDefinition[name] = prefixToPass + child.names[name];
}
break;
case 'expression':
setField(prefixToPass + child.as);
break;
case 'union':
case 'table':
if (child.sqlBatch) {
fieldDefinition[child.sqlBatch.parentKey.fieldName] = prefixToPass + child.sqlBatch.parentKey.as;
} else {
const definition = _defineObjectShape(node, prefixToPass, child);
setField(definition);
}
break;
case 'noop':
void 0;
break;
default:
throw new Error(`invalid SQLASTNode type: ${(0, _util.inspect)(child.type)}`);
}
}
for (let typeName in node.typedChildren || {}) {
const suffix = '@' + typeName;
for (let child of node.typedChildren[typeName]) {
const setField = definition => {
const originalKey = child.fieldName + suffix;
if ((0, _aliases.hasConflictingSiblings)(child, node.typedChildren[typeName]) && !child.sqlBatch) {
const aliasKey = (0, _aliases.getAliasKey)(originalKey, child.alias);
fieldDefinition[aliasKey] = definition;
fieldDefinition[originalKey] = {
column: '__jm__' + aliasKey,
type: () => _aliases.resolveAliasValue
};
} else {
fieldDefinition[originalKey] = definition;
}
};
switch (child.type) {
case 'column':
setField(prefixToPass + child.as);
break;
case 'composite':
setField(prefixToPass + child.as);
break;
case 'columnDeps':
for (let name in child.names) {
fieldDefinition[name + suffix] = prefixToPass + child.names[name];
}
break;
case 'expression':
setField(prefixToPass + child.as);
break;
case 'union':
case 'table':
if (child.sqlBatch) {
fieldDefinition[child.sqlBatch.parentKey.fieldName + suffix] = prefixToPass + child.sqlBatch.parentKey.as;
} else if ((_ref = child) != null ? (_ref = _ref.junction) != null ? _ref.sqlBatch : _ref : _ref) {
fieldDefinition[child.junction.sqlBatch.parentKey.fieldName + suffix] = prefixToPass + child.junction.sqlBatch.parentKey.as;
} else {
const definition = _defineObjectShape(node, prefixToPass, child);
setField(definition);
}
break;
case 'noop':
void 0;
break;
default:
throw new Error(`invalid SQLASTNode type: ${(0, _util.inspect)(child.type)}`);
}
}
}
if (node.grabMany) {
return [fieldDefinition];
}
return fieldDefinition;
}