Skip to content

Commit 4f235db

Browse files
author
Adam Lesniak
committed
feat(core): Add dateInRange
1 parent 297af86 commit 4f235db

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/default-schema.graphql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ type Employee {
2626
address: String @fake(type: streetAddress, options: { useFullAddress: true })
2727
subordinates: [Employee!] @listLength(min: 0, max: 3)
2828
company: Company
29+
workDates: [String!]
30+
@fake(
31+
type: dateInRange
32+
options: {
33+
dateFrom: "2023-11-05T16:56:52.372Z"
34+
dateTo: "2023-11-15T16:56:52.372Z",
35+
dateFormat: "YYYY-MM-DD"
36+
}
37+
)
2938
}
3039

3140
type Query {

src/fake.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function getRandomItem<T>(array: ReadonlyArray<T>): T {
1212
export const stdScalarFakers = {
1313
Int: () => faker.number.int({ min: 0, max: 99999 }),
1414
Float: () => faker.number.float({ min: 0, max: 99999, precision: 0.01 }),
15-
String: () => 'string',
15+
String: () => faker.word.sample(),
1616
Boolean: () => faker.datatype.boolean(),
1717
ID: () => toBase64(faker.number.int({ max: 9999999999 }).toString()),
1818
};
@@ -85,6 +85,17 @@ function fakeFunctions(fakerInstance: typeof faker) {
8585
.format(dateFormat)
8686
.toString(),
8787
},
88+
dateInRange: {
89+
args: ['dateFormat', 'dateFrom', 'dateTo'],
90+
func: (dateFormat, dateFrom, dateTo) =>
91+
Array.from({
92+
length: moment(dateTo).diff(moment(dateFrom), 'days'),
93+
}).map((_, index) =>
94+
moment(dateFrom).add(index, 'days')
95+
.format(dateFormat)
96+
.toString(),
97+
),
98+
},
8899
pastDate: {
89100
args: ['dateFormat'],
90101
func: (dateFormat) =>

src/fake_definition.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
6565
futureDate
6666
"Configure date format with option \`dateFormat\`"
6767
recentDate
68+
"Configure date format with option \`dateFormat\`"
69+
dateInRange
6870
6971
financeAccountName
7072
financeTransactionType
@@ -164,6 +166,8 @@ const fakeDefinitionAST = parse(/* GraphQL */ `
164166
dateFrom: String = "2010-01-01"
165167
"Only for types \`betweenDate\`. Example value: \`2038-01-19\`."
166168
dateTo: String = "2030-01-01"
169+
"Only for types \`betweenDate\`. Example value: \`2038-01-19\`."
170+
dateInRange: [String!]
167171
"Only for type \`colorHex\`. [Details here](https://stackoverflow.com/a/43235/4989887)"
168172
baseColor: fake__color = { red255: 0, green255: 0, blue255: 0 }
169173
"Only for type \`number\`"

src/fake_schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export const fakeFieldResolver: GraphQLFieldResolver<unknown, unknown> = async (
102102
}
103103

104104
if (isListType(type)) {
105+
if (Array.isArray(fakeValueOfType(type.ofType))) {
106+
return fakeValueOfType(type.ofType).sort();
107+
}
108+
105109
return Array(getListLength(fieldDef))
106110
.fill(null)
107111
.map(() => fakeValueOfType(type.ofType));

0 commit comments

Comments
 (0)