Skip to content

Commit

Permalink
Fake js version upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
sarpere committed Jun 17, 2023
1 parent 2f745d2 commit 94fdead
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand Down
45 changes: 21 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
76 changes: 38 additions & 38 deletions src/fake.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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<T>(array: ReadonlyArray<T>): T {
return array[getRandomInt(0, array.length - 1)];
}

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) {
Expand All @@ -23,44 +25,44 @@ 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(),
product: () => faker.commerce.product(),

// 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
Expand Down Expand Up @@ -125,7 +127,7 @@ const fakeFunctions = {
}

if (randomize === true) {
url += '#' + faker.datatype.number();
url += '#' + faker.number.int();
}

return url;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

0 comments on commit 94fdead

Please sign in to comment.