@graphql-tools/mock
Version:
A set of utils for faster development of GraphQL tools
55 lines (54 loc) • 1.98 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRootType = exports.takeRandom = exports.randomListLength = void 0;
exports.uuidv4 = uuidv4;
exports.makeRef = makeRef;
exports.isObject = isObject;
exports.copyOwnPropsIfNotPresent = copyOwnPropsIfNotPresent;
exports.copyOwnProps = copyOwnProps;
const utils_1 = require("@graphql-tools/utils");
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
// eslint-disable-next-line eqeqeq
const v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
const randomListLength = () => {
// Mocking has always returned list of length 2 by default
// return 1 + Math.round(Math.random() * 10)
return 2;
};
exports.randomListLength = randomListLength;
const takeRandom = (arr) => arr[Math.floor(Math.random() * arr.length)];
exports.takeRandom = takeRandom;
function makeRef(typeName, key) {
return { $ref: { key, typeName } };
}
function isObject(thing) {
return thing === Object(thing) && !Array.isArray(thing);
}
function copyOwnPropsIfNotPresent(target, source) {
for (const prop of Object.getOwnPropertyNames(source)) {
if (!Object.getOwnPropertyDescriptor(target, prop)) {
const propertyDescriptor = Object.getOwnPropertyDescriptor(source, prop);
Object.defineProperty(target, prop, propertyDescriptor == null ? {} : propertyDescriptor);
}
}
}
function copyOwnProps(target, ...sources) {
for (const source of sources) {
let chain = source;
while (chain != null) {
copyOwnPropsIfNotPresent(target, chain);
chain = Object.getPrototypeOf(chain);
}
}
return target;
}
const isRootType = (type, schema) => {
const rootTypeNames = (0, utils_1.getRootTypeNames)(schema);
return rootTypeNames.has(type.name);
};
exports.isRootType = isRootType;
;