Skip to content

Commit

Permalink
feat: add support for datetime faker
Browse files Browse the repository at this point in the history
This is a slightly cleverer version of faker.js'
datetime faker - it lets you pick two dates
between which you're generating a datetime
  • Loading branch information
swist committed Feb 28, 2022
1 parent 2775f15 commit d201193
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions src/default-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#
# Developed with ❤️ by APIs.guru | https://github.com/APIs-guru/graphql-faker

scalar DateTime

type Company {
id: ID
name: String @fake(type: companyName)
Expand All @@ -26,6 +28,7 @@ type Employee {
address: String @fake(type: streetAddress, options: { useFullAddress: true })
subordinates: [Employee!] @listLength(min: 0, max: 3)
company: Company
hiredAt: DateTime @fake(type: dateTime, options: {dateFrom: "2022-01-01", dateTo: "2022-02-28"})
}

type Query {
Expand Down
13 changes: 13 additions & 0 deletions src/fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ const fakeFunctions = {
args: ['dateFormat'],
func: (dateFormat) => moment(faker.date.recent()).format(dateFormat),
},
// DateTime section
dateTime: {
args: ['dateFormat', 'dateFrom', 'dateTo'],
func: (dateFormat, dateFrom, dateTo) => {
const date = moment(faker.date.between(dateFrom, dateTo));
const datetime = moment(faker.datatype.datetime());
return datetime.dayOfYear(date.dayOfYear())
.year(date.year())
.format(dateFormat)
.toString()

}
},

// Finance section
financeAccountName: () => faker.finance.accountName(),
Expand Down
9 changes: 8 additions & 1 deletion src/fake_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
hackerAbbreviation
hackerPhrase
"""
datetime
by default ISO8601
"""
dateTime
"An image url. Configure image with options: \`imageCategory\`, \`imageWidth\`, \`imageHeight\` and \`randomizeImageUrl\`"
imageUrl
Expand Down Expand Up @@ -194,7 +201,7 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
passwordLength: Int
"Only for type \`lorem\`"
loremSize: fake__loremSize
"Only for types \`*Date\`. Example value: \`YYYY MM DD\`. [Full Specification](http://momentjs.com/docs/#/displaying/format/)"
"Only for types \`*Date, dateTime\`. Example value: \`YYYY MM DD\`. [Full Specification](http://momentjs.com/docs/#/displaying/format/)"
dateFormat: String = "YYYY-MM-DDTHH:mm:ss[Z]"
"Only for types \`betweenDate\`. Example value: \`1986-11-02\`."
dateFrom: String = "2010-01-01"
Expand Down

0 comments on commit d201193

Please sign in to comment.