diff --git a/src/default-schema.graphql b/src/default-schema.graphql index 16d3abe..dd2d9de 100644 --- a/src/default-schema.graphql +++ b/src/default-schema.graphql @@ -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 { diff --git a/src/fake.ts b/src/fake.ts index 5a175e7..a2343f3 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -12,7 +12,7 @@ export function getRandomItem(array: ReadonlyArray): 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) => diff --git a/src/fake_definition.ts b/src/fake_definition.ts index 2147989..69762fb 100644 --- a/src/fake_definition.ts +++ b/src/fake_definition.ts @@ -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\`" diff --git a/src/fake_schema.ts b/src/fake_schema.ts index 6340384..1bdca44 100644 --- a/src/fake_schema.ts +++ b/src/fake_schema.ts @@ -102,6 +102,10 @@ export const fakeFieldResolver: GraphQLFieldResolver = 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));