From 0b5d95700f9ca59b30de6c3c1fd42b109b0f26c4 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:57:19 +0100 Subject: [PATCH 01/12] feat: Handle custom generators for faker --- src/index.ts | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index dfaba57..2bfa5d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { ASTKindToNode, ListTypeNode, NamedTypeNode, parse, printSchema, TypeNode } from 'graphql'; +import { faker } from '@faker-js/faker'; import casual from 'casual'; import { PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; import { pascalCase } from 'pascal-case'; @@ -101,7 +102,7 @@ const getScalarDefinition = (value: ScalarDefinition | ScalarGeneratorName): Sca return value; }; -const getCustomScalarValue = (customScalar: ScalarDefinition, opts: Options) => { +const getCasualCustomScalarValue = (customScalar: ScalarDefinition, opts: Options) => { // If there is a mapping to a `casual` type, then use it and make sure // to call it if it's a function const embeddedGenerator = casual[customScalar.generator]; @@ -128,6 +129,60 @@ const getCustomScalarValue = (customScalar: ScalarDefinition, opts: Options { + let embeddedGenerator: unknown = faker; + let dynamicGenerator = 'faker'; + + if (typeof generatorName === 'string') { + const generatorPath = generatorName.split('.'); + for (const key of generatorPath) { + if (embeddedGenerator.hasOwnProperty(key)) { + embeddedGenerator = embeddedGenerator[key]; + dynamicGenerator = `${dynamicGenerator}['${key}']`; + } + } + } + + // If the faker generator is not a function, we can assume the path is wrong + if (typeof embeddedGenerator === 'function') { + return [embeddedGenerator, dynamicGenerator]; + } + + return [null, null]; +}; + +const getFakerCustomScalarValue = (customScalar: ScalarDefinition, opts: Options) => { + // If there is a mapping to a `faker` type, then use it + const [embeddedGenerator, dynamicGenerator] = getFakerGenerators(customScalar.generator); + if (!embeddedGenerator && customScalar.generator) { + return customScalar.generator; + } + + const generatorArgs: unknown[] = Array.isArray(customScalar.arguments) + ? customScalar.arguments + : [customScalar.arguments]; + if (opts.dynamicValues) { + return `${dynamicGenerator}(...${JSON.stringify(generatorArgs)})`; + } + const value = embeddedGenerator(...generatorArgs); + + if (typeof value === 'string') { + return `'${value}'`; + } + if (typeof value === 'object') { + return `${JSON.stringify(value)}`; + } + return value; +}; + +const getCustomScalarValue = (customScalar: ScalarDefinition, opts: Options) => { + if (opts.generateLibrary === 'casual') { + return getCasualCustomScalarValue(customScalar, opts); + } + + return getFakerCustomScalarValue(customScalar, opts); +}; + const getNamedType = (opts: Options): string | number | boolean => { if (!opts.currentType) { return ''; From e3fe8e78f9f259bb0c6c78ec7481e478ee0afe04 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:57:54 +0100 Subject: [PATCH 02/12] test: Add coverage for faker custom generators --- tests/scalars/spec.ts | 48 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index c318967..1464fa5 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -1,7 +1,7 @@ import { plugin } from '../../src'; import testSchema from './schema'; -it('should generate custom scalars for native and custom types', async () => { +it('should generate custom scalars for native and custom types using casual', async () => { const result = await plugin(testSchema, [], { scalars: { String: 'string', @@ -45,3 +45,49 @@ it('should generate custom scalars for native and custom types', async () => { expect(result).toMatchSnapshot(); }); + +it('should generate custom scalars for native and custom types using faker', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'datatype.float', + arguments: [{ min: -100, max: 0}], + }, + ID: { + generator: 'datatype.number', + arguments: [{ min: 1, max: 100}], + }, + Boolean: 'false', + Int: { + generator: 'datatype.number', + arguments: [{ min: -100, max: 0}], + }, + AnyObject: 'internet.email', + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Corrupti qui incidunt eius consequatur qui.',", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + + expect(result).toMatchSnapshot(); +}); From a4b7d6092eac161153f9af645f8ff7789a499407 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:58:25 +0100 Subject: [PATCH 03/12] test: Update snapshots --- tests/scalars/__snapshots__/spec.ts.snap | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index c0b7a6b..2f26aa9 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should generate custom scalars for native and custom types 1`] = ` +exports[`should generate custom scalars for native and custom types using casual 1`] = ` " export const anA = (overrides?: Partial): A => { return { @@ -20,3 +20,24 @@ export const aB = (overrides?: Partial): B => { }; " `; + +exports[`should generate custom scalars for native and custom types using faker 1`] = ` +" +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Corrupti qui incidunt eius consequatur qui.', + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando64@gmail.com', + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93, + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51, + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; +" +`; From 626c92b1b56b4976a419300a61e84717641e2b09 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:10:29 +0100 Subject: [PATCH 04/12] chore: Update README.md --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f0ff221..cd900c2 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,13 @@ Changes the case of the enums. Accepts `upper-case#upperCase`, `pascal-case#pasc Allows you to define mappings for your custom scalars. Allows you to map any GraphQL Scalar to a [casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or -function key) with optional arguments +function key) with optional arguments, or a or [faker](https://fakerjs.dev/api/) generator with optional arguments + +Examples using **casual** -Examples **With arguments** -``` +```yaml plugins: - typescript-mock-data: scalars: @@ -60,7 +61,7 @@ plugins: **With multiple arguments** -``` +```yaml plugins: - typescript-mock-data: scalars: @@ -73,13 +74,48 @@ plugins: **Shorthand if you don't have arguments** -``` +```yaml plugins: - typescript-mock-data: scalars: Date: date # gets translated to casual.date() ``` +Examples using **faker** + +**With arguments** + +```yaml +plugins: + - typescript-mock-data: + scalars: + Date: # gets translated to faker.date.past(10) + generator: date.past + arguments: 10 +``` + +**With multiple arguments** + +```yaml +plugins: + - typescript-mock-data: + scalars: + Description: # gets translated to faker.lorem.paragraphs(3, '\n') + generator: lorem.paragraphs + arguments: + - 3 + - '\n' +``` + +**Shorthand if you don't have arguments** + +```yaml +plugins: + - typescript-mock-data: + scalars: + Date: date.past # gets translated to faker.date.past() +``` + **Custom value generator** ```yaml From 52e07d4ab3f4534b5742c0bf3583be3c899f0b2c Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:43:35 +0100 Subject: [PATCH 05/12] feat: Throw error if generator library is unknown --- src/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2bfa5d7..de9ba49 100644 --- a/src/index.ts +++ b/src/index.ts @@ -180,7 +180,12 @@ const getCustomScalarValue = (customScalar: ScalarDefinition, opts: Options): string | number | boolean => { From f525c69013f2f65ff38068fba9c8dd2c480b35db Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:52:30 +0100 Subject: [PATCH 06/12] chore: Add coverage for dynamicValue config --- tests/scalars/spec.ts | 93 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index 1464fa5..59ef5bf 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -46,6 +46,52 @@ it('should generate custom scalars for native and custom types using casual', as expect(result).toMatchSnapshot(); }); +it('should generate dynamic custom scalars for native and custom types using casual', async () => { + const result = await plugin(testSchema, [], { + dynamicValues: true, + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: 'email', + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),"); + + expect(result).toMatchSnapshot(); +}); + it('should generate custom scalars for native and custom types using faker', async () => { const result = await plugin(testSchema, [], { generateLibrary: 'faker', @@ -91,3 +137,50 @@ it('should generate custom scalars for native and custom types using faker', asy expect(result).toMatchSnapshot(); }); + +it('should generate dynamic custom scalars for native and custom types using faker', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + dynamicValues: true, + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'datatype.float', + arguments: [{ min: -100, max: 0}], + }, + ID: { + generator: 'datatype.number', + arguments: [{ min: 1, max: 100}], + }, + Boolean: 'false', + Int: { + generator: 'datatype.number', + arguments: [{ min: -100, max: 0}], + }, + AnyObject: 'internet.email', + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](...[]),", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\"min\":-100,\"max\":0}]),", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\"min\":1,\"max\":100}]),"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\"min\":-100,\"max\":0}]),"); + + expect(result).toMatchSnapshot(); +}); From d9dcf76f4ad55a7536a3bf24c83b3dca1b1b8c37 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:53:08 +0100 Subject: [PATCH 07/12] chore: Update snapshots --- tests/scalars/__snapshots__/spec.ts.snap | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 2f26aa9..893d707 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -41,3 +41,55 @@ export const aB = (overrides?: Partial): B => { }; " `; + +exports[`should generate dynamic custom scalars for native and custom types using casual 1`] = ` +"import casual from 'casual'; + +casual.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]), + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'], + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const seedMocks = (seed: number) => casual.seed(seed); +" +`; + +exports[`should generate dynamic custom scalars for native and custom types using faker 1`] = ` +"import { faker } from '@faker-js/faker'; + +faker.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\\"min\\":1,\\"max\\":100}]), + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](...[]), + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](...[]), + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\\"min\\":-100,\\"max\\":0}]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\\"min\\":-100,\\"max\\":0}]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const seedMocks = (seed: number) => faker.seed(seed); +" +`; From 680af53900b3baf0b9c0dc4c5694b61bc8d04aa3 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:56:29 +0100 Subject: [PATCH 08/12] revert changes to scalar tests --- tests/scalars/spec.ts | 93 ------------------------------------------- 1 file changed, 93 deletions(-) diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index 59ef5bf..1464fa5 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -46,52 +46,6 @@ it('should generate custom scalars for native and custom types using casual', as expect(result).toMatchSnapshot(); }); -it('should generate dynamic custom scalars for native and custom types using casual', async () => { - const result = await plugin(testSchema, [], { - dynamicValues: true, - scalars: { - String: 'string', - Float: { - generator: 'double', - arguments: [-100, 0], - }, - ID: { - generator: 'integer', - arguments: [1, 100], - }, - Boolean: 'false', - Int: { - generator: 'integer', - arguments: [-100, 0], - }, - AnyObject: 'email', - }, - }); - - expect(result).toBeDefined(); - - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],", - ); - - // Float - expect(result).toContain( - "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),", - ); - - // ID - expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),"); - - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - - // Int - expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),"); - - expect(result).toMatchSnapshot(); -}); - it('should generate custom scalars for native and custom types using faker', async () => { const result = await plugin(testSchema, [], { generateLibrary: 'faker', @@ -137,50 +91,3 @@ it('should generate custom scalars for native and custom types using faker', asy expect(result).toMatchSnapshot(); }); - -it('should generate dynamic custom scalars for native and custom types using faker', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'faker', - dynamicValues: true, - scalars: { - String: 'lorem.sentence', - Float: { - generator: 'datatype.float', - arguments: [{ min: -100, max: 0}], - }, - ID: { - generator: 'datatype.number', - arguments: [{ min: 1, max: 100}], - }, - Boolean: 'false', - Int: { - generator: 'datatype.number', - arguments: [{ min: -100, max: 0}], - }, - AnyObject: 'internet.email', - }, - }); - - expect(result).toBeDefined(); - - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](...[]),", - ); - - // Float - expect(result).toContain( - "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\"min\":-100,\"max\":0}]),", - ); - - // ID - expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\"min\":1,\"max\":100}]),"); - - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - - // Int - expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\"min\":-100,\"max\":0}]),"); - - expect(result).toMatchSnapshot(); -}); From aa801274fb06d90bb55693b22aaec39ef1a3a7d0 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:57:36 +0100 Subject: [PATCH 09/12] revert changes to snapshot file --- tests/scalars/__snapshots__/spec.ts.snap | 52 ------------------------ 1 file changed, 52 deletions(-) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 893d707..2f26aa9 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -41,55 +41,3 @@ export const aB = (overrides?: Partial): B => { }; " `; - -exports[`should generate dynamic custom scalars for native and custom types using casual 1`] = ` -"import casual from 'casual'; - -casual.seed(0); - -export const anA = (overrides?: Partial): A => { - return { - id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]), - str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'], - obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), - anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], - }; -}; - -export const aB = (overrides?: Partial): B => { - return { - int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]), - flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]), - bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, - }; -}; - -export const seedMocks = (seed: number) => casual.seed(seed); -" -`; - -exports[`should generate dynamic custom scalars for native and custom types using faker 1`] = ` -"import { faker } from '@faker-js/faker'; - -faker.seed(0); - -export const anA = (overrides?: Partial): A => { - return { - id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\\"min\\":1,\\"max\\":100}]), - str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](...[]), - obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), - anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](...[]), - }; -}; - -export const aB = (overrides?: Partial): B => { - return { - int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\\"min\\":-100,\\"max\\":0}]), - flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\\"min\\":-100,\\"max\\":0}]), - bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, - }; -}; - -export const seedMocks = (seed: number) => faker.seed(seed); -" -`; From b06849319a2e2f6cf162918a11afc215bac0f626 Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 17:04:27 +0100 Subject: [PATCH 10/12] chore: Add coverage for dynamicValue config --- tests/scalars/spec.ts | 93 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index 1464fa5..59ef5bf 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -46,6 +46,52 @@ it('should generate custom scalars for native and custom types using casual', as expect(result).toMatchSnapshot(); }); +it('should generate dynamic custom scalars for native and custom types using casual', async () => { + const result = await plugin(testSchema, [], { + dynamicValues: true, + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: 'email', + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),"); + + expect(result).toMatchSnapshot(); +}); + it('should generate custom scalars for native and custom types using faker', async () => { const result = await plugin(testSchema, [], { generateLibrary: 'faker', @@ -91,3 +137,50 @@ it('should generate custom scalars for native and custom types using faker', asy expect(result).toMatchSnapshot(); }); + +it('should generate dynamic custom scalars for native and custom types using faker', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + dynamicValues: true, + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'datatype.float', + arguments: [{ min: -100, max: 0}], + }, + ID: { + generator: 'datatype.number', + arguments: [{ min: 1, max: 100}], + }, + Boolean: 'false', + Int: { + generator: 'datatype.number', + arguments: [{ min: -100, max: 0}], + }, + AnyObject: 'internet.email', + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](...[]),", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\"min\":-100,\"max\":0}]),", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\"min\":1,\"max\":100}]),"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\"min\":-100,\"max\":0}]),"); + + expect(result).toMatchSnapshot(); +}); From 3db3df8b5551493712645e06970633397195ea8a Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Tue, 15 Nov 2022 17:05:28 +0100 Subject: [PATCH 11/12] chore: Update snapshots --- tests/scalars/__snapshots__/spec.ts.snap | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 2f26aa9..893d707 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -41,3 +41,55 @@ export const aB = (overrides?: Partial): B => { }; " `; + +exports[`should generate dynamic custom scalars for native and custom types using casual 1`] = ` +"import casual from 'casual'; + +casual.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]), + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'], + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const seedMocks = (seed: number) => casual.seed(seed); +" +`; + +exports[`should generate dynamic custom scalars for native and custom types using faker 1`] = ` +"import { faker } from '@faker-js/faker'; + +faker.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\\"min\\":1,\\"max\\":100}]), + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](...[]), + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](...[]), + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\\"min\\":-100,\\"max\\":0}]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\\"min\\":-100,\\"max\\":0}]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const seedMocks = (seed: number) => faker.seed(seed); +" +`; From d9f2ae991de09a50433562db82c7ee3787106d8d Mon Sep 17 00:00:00 2001 From: Thomas Sauques <39383180+Morphyish@users.noreply.github.com> Date: Thu, 17 Nov 2022 10:13:31 +0100 Subject: [PATCH 12/12] chore: Refactor code to fix lint issues --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index de9ba49..d6264cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,14 +129,14 @@ const getCasualCustomScalarValue = (customScalar: ScalarDefinition, opts: Option return value; }; -const getFakerGenerators = (generatorName: ScalarGeneratorName): [Function, string] => { +const getFakerGenerators = (generatorName: ScalarGeneratorName) => { let embeddedGenerator: unknown = faker; let dynamicGenerator = 'faker'; if (typeof generatorName === 'string') { const generatorPath = generatorName.split('.'); for (const key of generatorPath) { - if (embeddedGenerator.hasOwnProperty(key)) { + if (typeof embeddedGenerator === 'object' && key in embeddedGenerator) { embeddedGenerator = embeddedGenerator[key]; dynamicGenerator = `${dynamicGenerator}['${key}']`; } @@ -145,15 +145,15 @@ const getFakerGenerators = (generatorName: ScalarGeneratorName): [Function, stri // If the faker generator is not a function, we can assume the path is wrong if (typeof embeddedGenerator === 'function') { - return [embeddedGenerator, dynamicGenerator]; + return { embeddedGenerator, dynamicGenerator }; } - return [null, null]; + return { embeddedGenerator: null, dynamicGenerator: null }; }; const getFakerCustomScalarValue = (customScalar: ScalarDefinition, opts: Options) => { // If there is a mapping to a `faker` type, then use it - const [embeddedGenerator, dynamicGenerator] = getFakerGenerators(customScalar.generator); + const { embeddedGenerator, dynamicGenerator } = getFakerGenerators(customScalar.generator); if (!embeddedGenerator && customScalar.generator) { return customScalar.generator; }