diff --git a/README.md b/README.md index 2312f85..5e6306b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![npm](https://img.shields.io/npm/l/graphql-faker.svg)](https://github.com/graphql-kit/graphql-faker/blob/master/LICENSE) [![docker](https://img.shields.io/docker/build/apisguru/graphql-faker.svg)](https://hub.docker.com/r/apisguru/graphql-faker/) -Mock your future API or extend the existing API with realistic data from [faker.js](https://github.com/Marak/faker.js). **No coding required**. +Mock your future API or extend the existing API with realistic data from [faker.js](https://fakerjs.dev/). **No coding required**. All you need is to write [GraphQL SDL](https://alligator.io/graphql/graphql-sdl/). Don't worry, we will provide you with examples in our SDL editor. In the GIF below we add fields to types inside real GitHub API and you can make queries from GraphiQL, Apollo, Relay, etc. and receive **real data mixed with mock data.** diff --git a/package-lock.json b/package-lock.json index b5c45cb..12f0ce1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,12 @@ "version": "2.0.0-rc.25", "license": "MIT", "dependencies": { + "@faker-js/faker": "^8.0.2", "body-parser": "1.19.0", "chalk": "4.1.0", "cors": "2.8.5", "express": "4.17.1", "express-graphql": "0.12.0", - "faker": "5.5.3", "graphql": "14.7.0", "graphql-voyager": "1.0.0-rc.31", "moment": "2.29.1", @@ -28,7 +28,6 @@ "devDependencies": { "@types/body-parser": "1.19.0", "@types/cors": "2.8.10", - "@types/faker": "5.5.1", "@types/node": "20.2.5", "@types/react": "16.9.35", "@types/react-dom": "16.9.8", @@ -685,6 +684,21 @@ "@f/map-obj": "^1.2.2" } }, + "node_modules/@faker-js/faker": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.0.2.tgz", + "integrity": "sha512-Uo3pGspElQW91PCvKSIAXoEgAUlRnH29sX2/p89kg7sP1m2PzCufHINd0FhTXQf6DYGiUlVncdSPa2F9wxed2A==", + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/fakerjs" + } + ], + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0", + "npm": ">=6.14.13" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.8", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz", @@ -898,12 +912,6 @@ "integrity": "sha512-C7srjHiVG3Ey1nR6d511dtDkCEjxuN9W1HWAEjGq8kpcwmNM6JJkpC0xvabM7BXTG2wDq8Eu33iH9aQKa7IvLQ==", "dev": true }, - "node_modules/@types/faker": { - "version": "5.5.1", - "resolved": "https://registry.npmjs.org/@types/faker/-/faker-5.5.1.tgz", - "integrity": "sha512-JXGjV76oEUZUOSAr3bP5txETYoq0XDOQA8BpOz8Wc3EuvfF7sUVquf/EvM3aphuVKuVaYDSDu523/mAHnqrcvg==", - "dev": true - }, "node_modules/@types/glob": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz", @@ -5190,11 +5198,6 @@ "node": ">=0.10.0" } }, - "node_modules/faker": { - "version": "5.5.3", - "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz", - "integrity": "sha512-wLTv2a28wjUyWkbnX7u/ABZBkUkIF2fCd73V6P2oFqEGEktDfzWx4UxrSqtPRw0xPRAcjeAOIiJWqZm3pP4u3g==" - }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -14043,6 +14046,11 @@ "@f/map-obj": "^1.2.2" } }, + "@faker-js/faker": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.0.2.tgz", + "integrity": "sha512-Uo3pGspElQW91PCvKSIAXoEgAUlRnH29sX2/p89kg7sP1m2PzCufHINd0FhTXQf6DYGiUlVncdSPa2F9wxed2A==" + }, "@humanwhocodes/config-array": { "version": "0.11.8", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz", @@ -14204,12 +14212,6 @@ "integrity": "sha512-C7srjHiVG3Ey1nR6d511dtDkCEjxuN9W1HWAEjGq8kpcwmNM6JJkpC0xvabM7BXTG2wDq8Eu33iH9aQKa7IvLQ==", "dev": true }, - "@types/faker": { - "version": "5.5.1", - "resolved": "https://registry.npmjs.org/@types/faker/-/faker-5.5.1.tgz", - "integrity": "sha512-JXGjV76oEUZUOSAr3bP5txETYoq0XDOQA8BpOz8Wc3EuvfF7sUVquf/EvM3aphuVKuVaYDSDu523/mAHnqrcvg==", - "dev": true - }, "@types/glob": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz", @@ -17617,11 +17619,6 @@ } } }, - "faker": { - "version": "5.5.3", - "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz", - "integrity": "sha512-wLTv2a28wjUyWkbnX7u/ABZBkUkIF2fCd73V6P2oFqEGEktDfzWx4UxrSqtPRw0xPRAcjeAOIiJWqZm3pP4u3g==" - }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", diff --git a/package.json b/package.json index 28458a6..492940e 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,12 @@ "check:spelling": "cspell --cache --no-progress '**/*'" }, "dependencies": { + "@faker-js/faker": "^8.0.2", "body-parser": "1.19.0", "chalk": "4.1.0", "cors": "2.8.5", "express": "4.17.1", "express-graphql": "0.12.0", - "faker": "5.5.3", "graphql": "14.7.0", "graphql-voyager": "1.0.0-rc.31", "moment": "2.29.1", @@ -53,7 +53,6 @@ "devDependencies": { "@types/body-parser": "1.19.0", "@types/cors": "2.8.10", - "@types/faker": "5.5.1", "@types/node": "20.2.5", "@types/react": "16.9.35", "@types/react-dom": "16.9.8", diff --git a/src/fake.ts b/src/fake.ts index 11c533a..eaba7ce 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -1,8 +1,10 @@ -import * as faker from 'faker'; +import { allFakers } from '@faker-js/faker'; import * as moment from 'moment'; +const baseLocal = 'en'; +let faker = allFakers[baseLocal]; export function getRandomInt(min: number, max: number) { - return faker.datatype.number({ min, max }); + return faker.number.int({ min, max }); } export function getRandomItem(array: ReadonlyArray): T { @@ -10,11 +12,11 @@ export function getRandomItem(array: ReadonlyArray): T { } export const stdScalarFakers = { - Int: () => faker.datatype.number({ min: 0, max: 99999, precision: 1 }), - Float: () => faker.datatype.number({ min: 0, max: 99999, precision: 0.01 }), + Int: () => faker.number.float({ min: 0, max: 99999, precision: 1 }), + Float: () => faker.number.float({ min: 0, max: 99999, precision: 0.01 }), String: () => 'string', Boolean: () => faker.datatype.boolean(), - ID: () => toBase64(faker.datatype.number({ max: 9999999999 }).toString()), + ID: () => toBase64(faker.number.int({ max: 9999999999 }).toString()), }; function toBase64(str) { @@ -23,33 +25,33 @@ function toBase64(str) { const fakeFunctions = { // Address section - zipCode: () => faker.address.zipCode(), - city: () => faker.address.city(), + zipCode: () => faker.location.zipCode(), + city: () => faker.location.city(), // Skipped: faker.address.cityPrefix // Skipped: faker.address.citySuffix - streetName: () => faker.address.streetName(), + streetName: () => faker.location.street(), streetAddress: { args: ['useFullAddress'], - func: (useFullAddress) => faker.address.streetAddress(useFullAddress), + func: (useFullAddress) => faker.location.streetAddress(useFullAddress), }, // Skipped: faker.address.streetSuffix // Skipped: faker.address.streetPrefix - secondaryAddress: () => faker.address.secondaryAddress(), - county: () => faker.address.county(), - country: () => faker.address.country(), - countryCode: () => faker.address.countryCode(), - state: () => faker.address.state(), - stateAbbr: () => faker.address.stateAbbr(), - latitude: () => faker.address.latitude(), - longitude: () => faker.address.longitude(), + secondaryAddress: () => faker.location.secondaryAddress(), + county: () => faker.location.county(), + country: () => faker.location.country(), + countryCode: () => faker.location.countryCode(), + state: () => faker.location.state(), + stateAbbr: () => faker.location.state({ abbreviated: true }), + latitude: () => faker.location.latitude(), + longitude: () => faker.location.longitude(), // Commerce section - colorName: () => faker.commerce.color(), + colorName: () => faker.color.human(), productCategory: () => faker.commerce.department(), productName: () => faker.commerce.productName(), money: { args: ['minMoney', 'maxMoney', 'decimalPlaces'], - func: (min, max, dec) => faker.commerce.price(min, max, dec), + func: (min, max, dec) => faker.commerce.price({ min, max, dec }), }, // Skipped: faker.commerce.productAdjective productMaterial: () => faker.commerce.productMaterial(), @@ -57,10 +59,10 @@ const fakeFunctions = { // Company section // Skipped: faker.company.companySuffixes - companyName: () => faker.company.companyName(), + companyName: () => faker.company.name(), // Skipped: faker.company.companySuffix companyCatchPhrase: () => faker.company.catchPhrase(), - companyBs: () => faker.company.bs(), + companyBs: () => faker.company.buzzPhrase(), // Skipped: faker.company.catchPhraseAdjective // Skipped: faker.company.catchPhraseDescriptor // Skipped: faker.company.catchPhraseNoun @@ -125,7 +127,7 @@ const fakeFunctions = { } if (randomize === true) { - url += '#' + faker.datatype.number(); + url += '#' + faker.number.int(); } return url; @@ -162,26 +164,25 @@ const fakeFunctions = { }, // Name section - firstName: () => faker.name.firstName(), - lastName: () => faker.name.lastName(), - fullName: () => faker.name.findName(), - jobTitle: () => faker.name.jobTitle(), + firstName: () => faker.person.firstName(), + lastName: () => faker.person.lastName(), + fullName: () => faker.person.fullName(), + jobTitle: () => faker.person.jobTitle(), // Phone section - phoneNumber: () => faker.phone.phoneNumber(), + phoneNumber: () => faker.phone.number(), // Skipped: faker.phone.phoneNumberFormat // Skipped: faker.phone.phoneFormats // Random section number: { args: ['minNumber', 'maxNumber', 'precisionNumber'], - func: (min, max, precision) => - faker.datatype.number({ min, max, precision }), + func: (min, max, precision) => faker.number.float({ min, max, precision }), }, - uuid: () => faker.random.uuid(), - word: () => faker.random.word(), - words: () => faker.random.words(), - locale: () => faker.random.locale(), + uuid: () => faker.string.uuid(), + word: () => faker.lorem.word(), + words: () => faker.lorem.words(), + locale: () => faker.location.countryCode(), // System section // Skipped: faker.system.fileName @@ -201,15 +202,14 @@ Object.keys(fakeFunctions).forEach((key) => { fakeFunctions[key] = { args: [], func: value }; }); -export function fakeValue(type, options?, locale?) { +export function fakeValue(type, options?, locale?: string) { const fakeGenerator = fakeFunctions[type]; const argNames = fakeGenerator.args; //TODO: add check const callArgs = argNames.map((name) => options[name]); - - const localeBackup = faker.locale; - faker.setLocale(locale || localeBackup); + const desiredLocal = allFakers[locale || baseLocal]; + faker = desiredLocal; const result = fakeGenerator.func(...callArgs); - faker.setLocale(localeBackup); + faker = allFakers[baseLocal]; return result; }