Skip to content

Commit 94fdead

Browse files
committed
Fake js version upgrade
1 parent 2f745d2 commit 94fdead

File tree

4 files changed

+61
-65
lines changed

4 files changed

+61
-65
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![npm](https://img.shields.io/npm/l/graphql-faker.svg)](https://github.com/graphql-kit/graphql-faker/blob/master/LICENSE)
1010
[![docker](https://img.shields.io/docker/build/apisguru/graphql-faker.svg)](https://hub.docker.com/r/apisguru/graphql-faker/)
1111

12-
Mock your future API or extend the existing API with realistic data from [faker.js](https://github.com/Marak/faker.js). **No coding required**.
12+
Mock your future API or extend the existing API with realistic data from [faker.js](https://fakerjs.dev/). **No coding required**.
1313
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.
1414

1515
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.**

package-lock.json

Lines changed: 21 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
"check:spelling": "cspell --cache --no-progress '**/*'"
3838
},
3939
"dependencies": {
40+
"@faker-js/faker": "^8.0.2",
4041
"body-parser": "1.19.0",
4142
"chalk": "4.1.0",
4243
"cors": "2.8.5",
4344
"express": "4.17.1",
4445
"express-graphql": "0.12.0",
45-
"faker": "5.5.3",
4646
"graphql": "14.7.0",
4747
"graphql-voyager": "1.0.0-rc.31",
4848
"moment": "2.29.1",
@@ -53,7 +53,6 @@
5353
"devDependencies": {
5454
"@types/body-parser": "1.19.0",
5555
"@types/cors": "2.8.10",
56-
"@types/faker": "5.5.1",
5756
"@types/node": "20.2.5",
5857
"@types/react": "16.9.35",
5958
"@types/react-dom": "16.9.8",

src/fake.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import * as faker from 'faker';
1+
import { allFakers } from '@faker-js/faker';
22
import * as moment from 'moment';
3+
const baseLocal = 'en';
4+
let faker = allFakers[baseLocal];
35

46
export function getRandomInt(min: number, max: number) {
5-
return faker.datatype.number({ min, max });
7+
return faker.number.int({ min, max });
68
}
79

810
export function getRandomItem<T>(array: ReadonlyArray<T>): T {
911
return array[getRandomInt(0, array.length - 1)];
1012
}
1113

1214
export const stdScalarFakers = {
13-
Int: () => faker.datatype.number({ min: 0, max: 99999, precision: 1 }),
14-
Float: () => faker.datatype.number({ min: 0, max: 99999, precision: 0.01 }),
15+
Int: () => faker.number.float({ min: 0, max: 99999, precision: 1 }),
16+
Float: () => faker.number.float({ min: 0, max: 99999, precision: 0.01 }),
1517
String: () => 'string',
1618
Boolean: () => faker.datatype.boolean(),
17-
ID: () => toBase64(faker.datatype.number({ max: 9999999999 }).toString()),
19+
ID: () => toBase64(faker.number.int({ max: 9999999999 }).toString()),
1820
};
1921

2022
function toBase64(str) {
@@ -23,44 +25,44 @@ function toBase64(str) {
2325

2426
const fakeFunctions = {
2527
// Address section
26-
zipCode: () => faker.address.zipCode(),
27-
city: () => faker.address.city(),
28+
zipCode: () => faker.location.zipCode(),
29+
city: () => faker.location.city(),
2830
// Skipped: faker.address.cityPrefix
2931
// Skipped: faker.address.citySuffix
30-
streetName: () => faker.address.streetName(),
32+
streetName: () => faker.location.street(),
3133
streetAddress: {
3234
args: ['useFullAddress'],
33-
func: (useFullAddress) => faker.address.streetAddress(useFullAddress),
35+
func: (useFullAddress) => faker.location.streetAddress(useFullAddress),
3436
},
3537
// Skipped: faker.address.streetSuffix
3638
// Skipped: faker.address.streetPrefix
37-
secondaryAddress: () => faker.address.secondaryAddress(),
38-
county: () => faker.address.county(),
39-
country: () => faker.address.country(),
40-
countryCode: () => faker.address.countryCode(),
41-
state: () => faker.address.state(),
42-
stateAbbr: () => faker.address.stateAbbr(),
43-
latitude: () => faker.address.latitude(),
44-
longitude: () => faker.address.longitude(),
39+
secondaryAddress: () => faker.location.secondaryAddress(),
40+
county: () => faker.location.county(),
41+
country: () => faker.location.country(),
42+
countryCode: () => faker.location.countryCode(),
43+
state: () => faker.location.state(),
44+
stateAbbr: () => faker.location.state({ abbreviated: true }),
45+
latitude: () => faker.location.latitude(),
46+
longitude: () => faker.location.longitude(),
4547

4648
// Commerce section
47-
colorName: () => faker.commerce.color(),
49+
colorName: () => faker.color.human(),
4850
productCategory: () => faker.commerce.department(),
4951
productName: () => faker.commerce.productName(),
5052
money: {
5153
args: ['minMoney', 'maxMoney', 'decimalPlaces'],
52-
func: (min, max, dec) => faker.commerce.price(min, max, dec),
54+
func: (min, max, dec) => faker.commerce.price({ min, max, dec }),
5355
},
5456
// Skipped: faker.commerce.productAdjective
5557
productMaterial: () => faker.commerce.productMaterial(),
5658
product: () => faker.commerce.product(),
5759

5860
// Company section
5961
// Skipped: faker.company.companySuffixes
60-
companyName: () => faker.company.companyName(),
62+
companyName: () => faker.company.name(),
6163
// Skipped: faker.company.companySuffix
6264
companyCatchPhrase: () => faker.company.catchPhrase(),
63-
companyBs: () => faker.company.bs(),
65+
companyBs: () => faker.company.buzzPhrase(),
6466
// Skipped: faker.company.catchPhraseAdjective
6567
// Skipped: faker.company.catchPhraseDescriptor
6668
// Skipped: faker.company.catchPhraseNoun
@@ -125,7 +127,7 @@ const fakeFunctions = {
125127
}
126128

127129
if (randomize === true) {
128-
url += '#' + faker.datatype.number();
130+
url += '#' + faker.number.int();
129131
}
130132

131133
return url;
@@ -162,26 +164,25 @@ const fakeFunctions = {
162164
},
163165

164166
// Name section
165-
firstName: () => faker.name.firstName(),
166-
lastName: () => faker.name.lastName(),
167-
fullName: () => faker.name.findName(),
168-
jobTitle: () => faker.name.jobTitle(),
167+
firstName: () => faker.person.firstName(),
168+
lastName: () => faker.person.lastName(),
169+
fullName: () => faker.person.fullName(),
170+
jobTitle: () => faker.person.jobTitle(),
169171

170172
// Phone section
171-
phoneNumber: () => faker.phone.phoneNumber(),
173+
phoneNumber: () => faker.phone.number(),
172174
// Skipped: faker.phone.phoneNumberFormat
173175
// Skipped: faker.phone.phoneFormats
174176

175177
// Random section
176178
number: {
177179
args: ['minNumber', 'maxNumber', 'precisionNumber'],
178-
func: (min, max, precision) =>
179-
faker.datatype.number({ min, max, precision }),
180+
func: (min, max, precision) => faker.number.float({ min, max, precision }),
180181
},
181-
uuid: () => faker.random.uuid(),
182-
word: () => faker.random.word(),
183-
words: () => faker.random.words(),
184-
locale: () => faker.random.locale(),
182+
uuid: () => faker.string.uuid(),
183+
word: () => faker.lorem.word(),
184+
words: () => faker.lorem.words(),
185+
locale: () => faker.location.countryCode(),
185186

186187
// System section
187188
// Skipped: faker.system.fileName
@@ -201,15 +202,14 @@ Object.keys(fakeFunctions).forEach((key) => {
201202
fakeFunctions[key] = { args: [], func: value };
202203
});
203204

204-
export function fakeValue(type, options?, locale?) {
205+
export function fakeValue(type, options?, locale?: string) {
205206
const fakeGenerator = fakeFunctions[type];
206207
const argNames = fakeGenerator.args;
207208
//TODO: add check
208209
const callArgs = argNames.map((name) => options[name]);
209-
210-
const localeBackup = faker.locale;
211-
faker.setLocale(locale || localeBackup);
210+
const desiredLocal = allFakers[locale || baseLocal];
211+
faker = desiredLocal;
212212
const result = fakeGenerator.func(...callArgs);
213-
faker.setLocale(localeBackup);
213+
faker = allFakers[baseLocal];
214214
return result;
215215
}

0 commit comments

Comments
 (0)