UNPKG

graphql-mocks

Version:
96 lines (71 loc) 2.52 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var graphql = require('graphql'); var constants = require('./constants.js'); function concat(a, b) { return [].concat(a, b); } class FieldHighlighter { constructor(targets) { if (targets.length === 0) { targets = [[constants.HIGHLIGHT_ALL, constants.HIGHLIGHT_ALL]]; } this.targets = targets; } mark(schema) { return FieldHighlighter.expandTargets(schema, this.targets); } static expandTargets(schema, targets) { const fieldReferences = targets.map(target => { return FieldHighlighter.expandTarget(schema, target); }).reduce(concat, []); return fieldReferences; } static expandTarget(schema, [typeTarget, fieldTarget]) { var _type; if (typeTarget === constants.HIGHLIGHT_ALL) { const allTypes = Object.values(schema.getTypeMap()); const expanded = allTypes.filter(type => !type.name.startsWith('__')).map(type => { const hasFields = type && 'getFields' in type && !graphql.isInputType(type); return hasFields ? this.expandTarget(schema, [type.name, fieldTarget]) : undefined; }).filter(Boolean).reduce(concat, []); return expanded; } let type = schema.getType(typeTarget); if (!type) { return []; } if (typeTarget === constants.HIGHLIGHT_ROOT_QUERY) { const queryType = schema.getQueryType(); if (!queryType) { return []; } type = queryType; } if (typeTarget === constants.HIGHLIGHT_ROOT_MUTATION) { const mutationType = schema.getMutationType(); if (!mutationType) { return []; } type = mutationType; } if (!type || !((_type = type) === null || _type === void 0 ? void 0 : _type.name) || !('getFields' in type)) { return []; } const fields = type.getFields(); const fieldNames = fieldTarget === constants.HIGHLIGHT_ALL ? Object.keys(fields) : [fieldTarget]; const fieldReferences = fieldNames.filter(fieldName => Object.keys(fields).includes(fieldName)).map(fieldName => { var _type2; return ((_type2 = type) === null || _type2 === void 0 ? void 0 : _type2.name) && fieldName ? [type.name, fieldName] : undefined; }).filter(Boolean); return fieldReferences; } } const field = function type(...targetReferences) { return new FieldHighlighter(targetReferences); }; exports.FieldHighlighter = FieldHighlighter; exports.field = field; //# sourceMappingURL=field.js.map