Skip to content

Commit

Permalink
feat(core): Add dateInRange
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Lesniak committed Nov 6, 2023
1 parent 297af86 commit 4f235db
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/default-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion src/fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
};
Expand Down Expand Up @@ -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) =>
Expand Down
4 changes: 4 additions & 0 deletions src/fake_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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\`"
Expand Down
4 changes: 4 additions & 0 deletions src/fake_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 4f235db

Please sign in to comment.