UNPKG

graphql-mocks

Version:

Tools for setting up graphql test resolvers

84 lines (82 loc) 3.04 kB
'use strict'; function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var graphql = require('graphql'); var constants = require('./constants.js'); class FieldHighlighter { constructor(targets) { _defineProperty(this, "targets", void 0); 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 = []; for (const target of targets) { const fr = FieldHighlighter.expandTarget(schema, target); for (const fieldReference of fr) { fieldReferences.push(fieldReference); } } return fieldReferences; } static expandTarget(schema, [typeTarget, fieldTarget]) { var _type; if (typeTarget === constants.HIGHLIGHT_ALL) { const references = []; const types = schema.getTypeMap(); for (const typeName in types) { if (typeName.startsWith('__')) { continue; } const type = types[typeName]; if (!graphql.isInterfaceType(type) && !graphql.isObjectType(type)) { continue; } references.push([typeName, fieldTarget]); } return this.expandTargets(schema, references); } 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 && _type.name) || !('getFields' in type)) { return []; } const fields = type.getFields(); const fieldNames = fieldTarget === constants.HIGHLIGHT_ALL ? Object.keys(fields) : [fieldTarget]; const fieldReferences = []; for (const fieldName of fieldNames) { if (fields[fieldName]) { fieldReferences.push([type.name, fieldName]); } } return fieldReferences; } } const field = function type(...targetReferences) { return new FieldHighlighter(targetReferences); }; exports.FieldHighlighter = FieldHighlighter; exports.field = field; //# sourceMappingURL=field.js.map