UNPKG

graphiql

Version:
127 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fillLeafs = void 0; var graphql_1 = require("graphql"); function fillLeafs(schema, docString, getDefaultFieldNames) { var insertions = []; if (!schema || !docString) { return { insertions: insertions, result: docString }; } var ast; try { ast = graphql_1.parse(docString); } catch (error) { return { insertions: insertions, result: docString }; } var fieldNameFn = getDefaultFieldNames || defaultGetDefaultFieldNames; var typeInfo = new graphql_1.TypeInfo(schema); graphql_1.visit(ast, { leave: function (node) { typeInfo.leave(node); }, enter: function (node) { typeInfo.enter(node); if (node.kind === 'Field' && !node.selectionSet) { var fieldType = typeInfo.getType(); var selectionSet = buildSelectionSet(isFieldType(fieldType), fieldNameFn); if (selectionSet && node.loc) { var indent = getIndentation(docString, node.loc.start); insertions.push({ index: node.loc.end, string: ' ' + graphql_1.print(selectionSet).replace(/\n/g, '\n' + indent), }); } } }, }); return { insertions: insertions, result: withInsertions(docString, insertions), }; } exports.fillLeafs = fillLeafs; function defaultGetDefaultFieldNames(type) { if (!('getFields' in type)) { return []; } var fields = type.getFields(); if (fields.id) { return ['id']; } if (fields.edges) { return ['edges']; } if (fields.node) { return ['node']; } var leafFieldNames = []; Object.keys(fields).forEach(function (fieldName) { if (graphql_1.isLeafType(fields[fieldName].type)) { leafFieldNames.push(fieldName); } }); return leafFieldNames; } function buildSelectionSet(type, getDefaultFieldNames) { var namedType = graphql_1.getNamedType(type); if (!type || graphql_1.isLeafType(type)) { return; } var fieldNames = getDefaultFieldNames(namedType); if (!Array.isArray(fieldNames) || fieldNames.length === 0 || !('getFields' in namedType)) { return; } return { kind: 'SelectionSet', selections: fieldNames.map(function (fieldName) { var fieldDef = namedType.getFields()[fieldName]; var fieldType = fieldDef ? fieldDef.type : null; return { kind: 'Field', name: { kind: 'Name', value: fieldName, }, selectionSet: buildSelectionSet(fieldType, getDefaultFieldNames), }; }), }; } function withInsertions(initial, insertions) { if (insertions.length === 0) { return initial; } var edited = ''; var prevIndex = 0; insertions.forEach(function (_a) { var index = _a.index, string = _a.string; edited += initial.slice(prevIndex, index) + string; prevIndex = index; }); edited += initial.slice(prevIndex); return edited; } function getIndentation(str, index) { var indentStart = index; var indentEnd = index; while (indentStart) { var c = str.charCodeAt(indentStart - 1); if (c === 10 || c === 13 || c === 0x2028 || c === 0x2029) { break; } indentStart--; if (c !== 9 && c !== 11 && c !== 12 && c !== 32 && c !== 160) { indentEnd = indentStart; } } return str.substring(indentStart, indentEnd); } function isFieldType(fieldType) { if (fieldType) { return fieldType; } } //# sourceMappingURL=fillLeafs.js.map