Skip to content

Commit d201193

Browse files
committed
feat: add support for datetime faker
This is a slightly cleverer version of faker.js' datetime faker - it lets you pick two dates between which you're generating a datetime
1 parent 2775f15 commit d201193

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/default-schema.graphql

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#
1212
# Developed with ❤️ by APIs.guru | https://github.com/APIs-guru/graphql-faker
1313

14+
scalar DateTime
15+
1416
type Company {
1517
id: ID
1618
name: String @fake(type: companyName)
@@ -26,6 +28,7 @@ type Employee {
2628
address: String @fake(type: streetAddress, options: { useFullAddress: true })
2729
subordinates: [Employee!] @listLength(min: 0, max: 3)
2830
company: Company
31+
hiredAt: DateTime @fake(type: dateTime, options: {dateFrom: "2022-01-01", dateTo: "2022-02-28"})
2932
}
3033

3134
type Query {

src/fake.ts

+13
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ const fakeFunctions = {
9494
args: ['dateFormat'],
9595
func: (dateFormat) => moment(faker.date.recent()).format(dateFormat),
9696
},
97+
// DateTime section
98+
dateTime: {
99+
args: ['dateFormat', 'dateFrom', 'dateTo'],
100+
func: (dateFormat, dateFrom, dateTo) => {
101+
const date = moment(faker.date.between(dateFrom, dateTo));
102+
const datetime = moment(faker.datatype.datetime());
103+
return datetime.dayOfYear(date.dayOfYear())
104+
.year(date.year())
105+
.format(dateFormat)
106+
.toString()
107+
108+
}
109+
},
97110

98111
// Finance section
99112
financeAccountName: () => faker.finance.accountName(),

src/fake_definition.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
113113
114114
hackerAbbreviation
115115
hackerPhrase
116+
117+
"""
118+
datetime
119+
by default ISO8601
120+
"""
121+
dateTime
122+
116123
117124
"An image url. Configure image with options: \`imageCategory\`, \`imageWidth\`, \`imageHeight\` and \`randomizeImageUrl\`"
118125
imageUrl
@@ -194,7 +201,7 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
194201
passwordLength: Int
195202
"Only for type \`lorem\`"
196203
loremSize: fake__loremSize
197-
"Only for types \`*Date\`. Example value: \`YYYY MM DD\`. [Full Specification](http://momentjs.com/docs/#/displaying/format/)"
204+
"Only for types \`*Date, dateTime\`. Example value: \`YYYY MM DD\`. [Full Specification](http://momentjs.com/docs/#/displaying/format/)"
198205
dateFormat: String = "YYYY-MM-DDTHH:mm:ss[Z]"
199206
"Only for types \`betweenDate\`. Example value: \`1986-11-02\`."
200207
dateFrom: String = "2010-01-01"

0 commit comments

Comments
 (0)