Skip to content

feat: add support for datetime faker #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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