@gqlts/runtime
Version:
Gqlts runtime client
36 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFieldFromPath = getFieldFromPath;
const tslib_1 = require("tslib");
const lodash_startswith_1 = tslib_1.__importDefault(require("lodash.startswith"));
function getFieldFromPath(root, path) {
let current;
if (!root)
throw new Error('root type is not provided');
if (path.length === 0)
throw new Error(`path is empty`);
path.forEach((f) => {
const type = current ? current.type : root;
if (!type.fields)
throw new Error(`type \`${type.name}\` does not have fields`);
const possibleTypes = Object.keys(type.fields)
.filter((i) => (0, lodash_startswith_1.default)(i, 'on_'))
.reduce((types, fieldName) => {
const field = type.fields && type.fields[fieldName];
if (field)
types.push(field.type);
return types;
}, [type]);
let field = null;
possibleTypes.forEach((type) => {
const found = type.fields && type.fields[f];
if (found)
field = found;
});
if (!field)
throw new Error(`type \`${type.name}\` does not have a field \`${f}\``);
current = field;
});
return current;
}
//# sourceMappingURL=getFieldFromPath.js.map