UNPKG

@graphql-mocks/falso

Version:

Fake GraphQL queries with graphql-mocks and Falso

98 lines (97 loc) 5.35 kB
import { unwrap, hasListType, listItemType } from 'graphql-mocks/graphql/type-utils'; import * as falso from '@ngneat/falso'; import { isNonNullType, isEnumType, isObjectType, isAbstractType } from 'graphql'; import { guessFalsoFn } from './guess-falso-fn.mjs'; function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min) + min); } function booleanChance(percentage) { return getRandomInt(0, 100) < Math.floor(percentage); } function falsoFieldResolver(options) { return function internalFalsoResolver(parent, _args, _context, info) { var _info$parentType, _options$fields, _options$fields$paren, _fieldOptions$possibl, _ref, _fieldOptions$nullPer, _ref2, _fieldOptions$nullLis, _ref3, _fieldOptions$listCou; var parentTypeName = (_info$parentType = info.parentType) === null || _info$parentType === void 0 ? void 0 : _info$parentType.name; var { fieldName, returnType } = info; if (parent && fieldName in parent) { return parent[fieldName]; } var unwrappedReturnType = unwrap(returnType); var isList = hasListType(returnType); var isNonNull = isNonNullType(returnType); var fieldOptions = (_options$fields = options.fields) === null || _options$fields === void 0 ? void 0 : (_options$fields$paren = _options$fields[parentTypeName]) === null || _options$fields$paren === void 0 ? void 0 : _options$fields$paren[fieldName]; var fieldValues = (_fieldOptions$possibl = fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.possibleValues) !== null && _fieldOptions$possibl !== void 0 ? _fieldOptions$possibl : []; var nullPercentage = (_ref = (_fieldOptions$nullPer = fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.nullPercentage) !== null && _fieldOptions$nullPer !== void 0 ? _fieldOptions$nullPer : options === null || options === void 0 ? void 0 : options.nullPercentage) !== null && _ref !== void 0 ? _ref : 10; var nullListPercentage = (_ref2 = (_fieldOptions$nullLis = fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.nullListPercentage) !== null && _fieldOptions$nullLis !== void 0 ? _fieldOptions$nullLis : options === null || options === void 0 ? void 0 : options.nullListPercentage) !== null && _ref2 !== void 0 ? _ref2 : nullPercentage; var [defaultMin, defaultMax] = [0, 10]; var listCountOption = (_ref3 = (_fieldOptions$listCou = fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.listCount) !== null && _fieldOptions$listCou !== void 0 ? _fieldOptions$listCou : options === null || options === void 0 ? void 0 : options.listCount) !== null && _ref3 !== void 0 ? _ref3 : { min: defaultMin, max: defaultMax }; var { min, max } = typeof listCountOption === 'number' ? { min: listCountOption, max: listCountOption } : listCountOption; var listCount = getRandomInt(min, max); if (isEnumType(unwrappedReturnType)) { fieldValues = unwrappedReturnType.getValues().map(value => value.value); } var getValue = (allowNull, nullPercentage) => { var _options, _options2; var value = null; var options = [...fieldValues].map(option => option == null ? null : option); if ((_options = options) !== null && _options !== void 0 && _options.length && !allowNull) { options = options.filter(option => option != null); } if ((_options2 = options) !== null && _options2 !== void 0 && _options2.length) { // use random option from specified values value = falso.rand(options); } else if (typeof (fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.falsoFn) === 'string') { // use a specified falso function var { falsoFn } = fieldOptions; // eslint-disable-next-line @typescript-eslint/no-explicit-any if (!falso[falsoFn]) { throw new Error("Could not find falso function at \"falso.".concat(falsoFn, "\", double-check the falso docs")); } // eslint-disable-next-line @typescript-eslint/no-explicit-any var fn = falso[falsoFn]; value = fn(); } else { value = guessFalsoFn(fieldName, returnType)(); } if (allowNull) { value = booleanChance(nullPercentage) ? null : value; } else if (value == null) { value = guessFalsoFn(fieldName, returnType)(); } return value; }; if (isObjectType(unwrappedReturnType) || isAbstractType(unwrappedReturnType)) { // handles list case where the *number* to resolve is determined here // but the actual data of each field is handled in follow up recursive // resolving for each individual field. if (isList) { return new Array(listCount).fill({}); } // otherwise, return and let future resolvers figure // out the scalar field data return {}; } if (isList) { var allowNullListItems = !isNonNullType(listItemType(returnType)); return !isNonNull && booleanChance(nullPercentage) ? null : new Array(listCount).fill(null).map(() => getValue(allowNullListItems, nullListPercentage)); } else { return getValue(!isNonNull, nullPercentage); } }; } export { falsoFieldResolver }; //# sourceMappingURL=falso-field-resolver.mjs.map