Skip to content
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

feat(core): Add dateInRange fake #203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/default-schema.graphql
Original file line number Diff line number Diff line change
@@ -26,6 +26,15 @@ type Employee {
address: String @fake(type: streetAddress, options: { useFullAddress: true })
subordinates: [Employee!] @listLength(min: 0, max: 3)
company: Company
workDates: [String!]
@fake(
type: dateInRange
options: {
dateFrom: "2023-11-05T16:56:52.372Z"
dateTo: "2023-11-15T16:56:52.372Z",
dateFormat: "YYYY-MM-DD"
}
)
}

type Query {
13 changes: 12 additions & 1 deletion src/fake.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export function getRandomItem<T>(array: ReadonlyArray<T>): T {
export const stdScalarFakers = {
Int: () => faker.number.int({ min: 0, max: 99999 }),
Float: () => faker.number.float({ min: 0, max: 99999, precision: 0.01 }),
String: () => 'string',
String: () => faker.word.sample(),
Boolean: () => faker.datatype.boolean(),
ID: () => toBase64(faker.number.int({ max: 9999999999 }).toString()),
};
@@ -85,6 +85,17 @@ function fakeFunctions(fakerInstance: typeof faker) {
.format(dateFormat)
.toString(),
},
dateInRange: {
args: ['dateFormat', 'dateFrom', 'dateTo'],
func: (dateFormat, dateFrom, dateTo) =>
Array.from({
length: moment(dateTo).diff(moment(dateFrom), 'days'),
}).map((_, index) =>
moment(dateFrom).add(index, 'days')
.format(dateFormat)
.toString(),
),
},
pastDate: {
args: ['dateFormat'],
func: (dateFormat) =>
4 changes: 4 additions & 0 deletions src/fake_definition.ts
Original file line number Diff line number Diff line change
@@ -65,6 +65,8 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
futureDate
"Configure date format with option \`dateFormat\`"
recentDate
"Configure date format with option \`dateFormat\`"
dateInRange
financeAccountName
financeTransactionType
@@ -164,6 +166,8 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
dateFrom: String = "2010-01-01"
"Only for types \`betweenDate\`. Example value: \`2038-01-19\`."
dateTo: String = "2030-01-01"
"Only for types \`betweenDate\`. Example value: \`2038-01-19\`."
dateInRange: [String!]
"Only for type \`colorHex\`. [Details here](https://stackoverflow.com/a/43235/4989887)"
baseColor: fake__color = { red255: 0, green255: 0, blue255: 0 }
"Only for type \`number\`"
4 changes: 4 additions & 0 deletions src/fake_schema.ts
Original file line number Diff line number Diff line change
@@ -102,6 +102,10 @@ export const fakeFieldResolver: GraphQLFieldResolver<unknown, unknown> = async (
}

if (isListType(type)) {
if (Array.isArray(fakeValueOfType(type.ofType))) {
return fakeValueOfType(type.ofType).sort();
}

return Array(getListLength(fieldDef))
.fill(null)
.map(() => fakeValueOfType(type.ofType));