graphql-mocks
Version:
Tools for setting up graphql test resolvers
44 lines (42 loc) • 2.28 kB
JavaScript
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 constants = require('./constants.js');
class TypeHighlighter {
constructor(targets) {
_defineProperty(this, "targets", void 0);
if (targets.length === 0) {
targets = [constants.HIGHLIGHT_ALL];
}
this.targets = targets;
}
mark(schema) {
return TypeHighlighter.expandTargets(schema, this.targets);
}
static expandTargets(schema, targets) {
var _schema$getQueryType, _schema$getMutationTy;
const allTypeNames = Object.keys(schema.getTypeMap());
if (targets.includes(constants.HIGHLIGHT_ALL)) {
return allTypeNames;
}
const hasSpecialQueryTarget = targets.includes(constants.HIGHLIGHT_ROOT_QUERY);
const hasSpecialMutationTarget = targets.includes(constants.HIGHLIGHT_ROOT_MUTATION);
targets = targets.filter(target => target !== constants.HIGHLIGHT_ROOT_QUERY && target !== constants.HIGHLIGHT_ROOT_MUTATION);
const queryTypeName = (_schema$getQueryType = schema.getQueryType()) === null || _schema$getQueryType === void 0 ? void 0 : _schema$getQueryType.name;
if (queryTypeName && hasSpecialQueryTarget) {
targets.push(queryTypeName);
}
const queryMutationName = (_schema$getMutationTy = schema.getMutationType()) === null || _schema$getMutationTy === void 0 ? void 0 : _schema$getMutationTy.name;
if (queryMutationName && hasSpecialMutationTarget) {
targets.push(queryMutationName);
}
return targets.filter(target => allTypeNames.includes(target));
}
}
const type = function type(...targetReferences) {
return new TypeHighlighter(targetReferences);
};
exports.TypeHighlighter = TypeHighlighter;
exports.type = type;
//# sourceMappingURL=type.js.map
;