From 66683e1a6abdf7480be2ca4cb475abe06c87c599 Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 10:12:35 -0600 Subject: [PATCH 1/9] add input to scalar spec schema, update snapshot --- tests/scalars/__snapshots__/spec.ts.snap | 24 ++++++++++++++++++++++++ tests/scalars/schema.ts | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 4a01a65..410a876 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -18,6 +18,12 @@ export const aB = (overrides?: Partial): B => { bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, }; }; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Gianni_Kutch@hotmail.com', + }; +}; " `; @@ -39,6 +45,12 @@ export const aB = (overrides?: Partial): B => { bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, }; }; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Maia49@hotmail.com', + }; +}; " `; @@ -64,6 +76,12 @@ export const aB = (overrides?: Partial): B => { }; }; +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + }; +}; + export const seedMocks = (seed: number) => casual.seed(seed); " `; @@ -90,6 +108,12 @@ export const aB = (overrides?: Partial): B => { }; }; +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](), + }; +}; + export const seedMocks = (seed: number) => faker.seed(seed); " `; diff --git a/tests/scalars/schema.ts b/tests/scalars/schema.ts index c470b74..6c4469f 100644 --- a/tests/scalars/schema.ts +++ b/tests/scalars/schema.ts @@ -15,4 +15,8 @@ export default buildSchema(/* GraphQL */ ` flt: Float! bool: Boolean! } + + input C { + anyObject: AnyObject! + } `); From 7b9e00086fa81158b3773266b460499b3f7475a9 Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 10:23:14 -0600 Subject: [PATCH 2/9] rearrange specs just trying to group them in preparation for adding more specs with the new config options for scalars --- tests/scalars/__snapshots__/spec.ts.snap | 46 ++-- tests/scalars/spec.ts | 310 ++++++++++++----------- 2 files changed, 181 insertions(+), 175 deletions(-) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 410a876..8dbd200 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 using casual 1`] = ` +exports[`custom scalar generation for native and custom types using casual should generate custom scalars for native and custom types using casual 1`] = ` " export const anA = (overrides?: Partial): A => { return { @@ -27,66 +27,66 @@ export const aC = (overrides?: Partial): C => { " `; -exports[`should generate custom scalars for native and custom types using faker 1`] = ` -" +exports[`custom scalar generation for native and custom types using casual 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! : 83, - str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.', + 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! : 'Orlando_Cremin@gmail.com', + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], }; }; export const aB = (overrides?: Partial): B => { return { - int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93, - flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51, + 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 aC = (overrides?: Partial): C => { return { - anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Maia49@hotmail.com', + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], }; }; + +export const seedMocks = (seed: number) => casual.seed(seed); " `; -exports[`should generate dynamic custom scalars for native and custom types using casual 1`] = ` -"import casual from 'casual'; - -casual.seed(0); - +exports[`custom scalar generation for native and custom types using faker 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! : casual['integer'](...[1,100]), - str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'], + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.', obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), - anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@gmail.com', }; }; 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]), + 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, }; }; export const aC = (overrides?: Partial): C => { return { - anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Maia49@hotmail.com', }; }; - -export const seedMocks = (seed: number) => casual.seed(seed); " `; -exports[`should generate dynamic custom scalars for native and custom types using faker 1`] = ` +exports[`custom scalar generation for native and custom types using faker should generate dynamic custom scalars for native and custom types using faker 1`] = ` "import { fakerEN as faker } from '@faker-js/faker'; faker.seed(0); diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index ce9f4dd..0dcf060 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -1,192 +1,198 @@ import { plugin } from '../../src'; import testSchema from './schema'; -it('should generate custom scalars for native and custom types using casual', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'casual', - scalars: { - String: 'string', - Float: { - generator: 'double', - arguments: [-100, 0], +describe('Custom scalar generation using casual', () => { + it('should generate custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + 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', }, - ID: { - generator: 'integer', - arguments: [1, 100], - }, - Boolean: 'false', - Int: { - generator: 'integer', - arguments: [-100, 0], - }, - AnyObject: 'email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',", - ); + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',", + ); - // Float - expect(result).toContain( - "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,", - ); + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,", + ); - // ID - expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,"); + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,"); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); - expect(result).toMatchSnapshot(); -}); + expect(result).toMatchSnapshot(); + }); -it('should generate dynamic custom scalars for native and custom types using casual', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'casual', - dynamicValues: true, - scalars: { - String: 'string', - Float: { - generator: 'double', - arguments: [-100, 0], + it('should generate dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + 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', }, - ID: { - generator: 'integer', - arguments: [1, 100], - }, - Boolean: 'false', - Int: { - generator: 'integer', - arguments: [-100, 0], - }, - AnyObject: 'email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain("str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],"); + // 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]),", - ); + // 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]),", - ); + // 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"); + // 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]),", - ); + // Int + expect(result).toContain( + "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),", + ); - expect(result).toMatchSnapshot(); + 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: 'number.float', - arguments: [{ min: -100, max: 0, fractionDigits: 2 }], +describe('custom scalar generation using faker', () => { + it('should generate custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0, fractionDigits: 2 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: 'internet.email', }, - ID: { - generator: 'number.int', - arguments: [{ min: 1, max: 100 }], - }, - Boolean: 'false', - Int: { - generator: 'number.int', - arguments: [{ min: -100, max: 0 }], - }, - AnyObject: 'internet.email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',", - ); + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',", + ); - // Float - expect(result).toContain("flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,"); + // 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,"); + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,"); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); - expect(result).toMatchSnapshot(); -}); + 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: 'number.float', - arguments: [{ min: -100, max: 0 }], + it('should generate dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + dynamicValues: true, + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: 'internet.email', }, - ID: { - generator: 'number.int', - arguments: [{ min: 1, max: 100 }], - }, - Boolean: 'false', - Int: { - generator: 'number.int', - arguments: [{ min: -100, max: 0 }], - }, - AnyObject: 'internet.email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),", - ); + // 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['number']['float'](...[{\"min\":-100,\"max\":0}]),", - ); + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\"min\":-100,\"max\":0}]),", + ); - // ID - expect(result).toContain( - "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"min\":1,\"max\":100}]),", - ); + // ID + expect(result).toContain( + "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"min\":1,\"max\":100}]),", + ); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain( - "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\"min\":-100,\"max\":0}]),", - ); + // Int + expect(result).toContain( + "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\"min\":-100,\"max\":0}]),", + ); - expect(result).toMatchSnapshot(); + expect(result).toMatchSnapshot(); + }); }); From bfa5db29a2f3ac16ff292b23ae68fc7a0dab7c5c Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 12:48:18 -0600 Subject: [PATCH 3/9] Add generatorMode to Options type, pass an appropriate value per node type So, when we have a normal FieldDefinition, we want to assume we want to use the output generator. When we visit the InputObjectTypeDefinition and call generateMockValue on its children, we can assume we want the input generator. We keep the other types of generator config types around for backwards compatibility. It could be a nice refactoring opportunity to map the existing config types to the input/output type, instead of what we do in this commit, which is check/switch on `value` in `getGeneratorDefinition`, but I wanted to prove to myself that we could generate distinct mocks. --- src/index.ts | 53 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index b46a01a..8cc66df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string; type Options = { typeName: string; fieldName: string; + generatorMode: 'input' | 'output'; types: TypeItem[]; typeNamesConvention: NamingConvention; enumValuesConvention: NamingConvention; @@ -90,13 +91,21 @@ const hashedString = (value: string) => { return hash; }; -const getGeneratorDefinition = (value: GeneratorDefinition | GeneratorName): GeneratorDefinition => { +const getGeneratorDefinition = ( + value: GeneratorOptions | InputOutputGeneratorOptions, + generatorMode: Options['generatorMode'], +): GeneratorDefinition => { if (typeof value === 'string') { return { generator: value, arguments: [], }; } + + if ('input' in value && 'output' in value) { + return getGeneratorDefinition(value[generatorMode], generatorMode); + } + return value; }; @@ -246,12 +255,18 @@ const handleValueGeneration = ( if (opts.fieldGeneration) { // Check for a specific generation for the type & field if (opts.typeName in opts.fieldGeneration && opts.fieldName in opts.fieldGeneration[opts.typeName]) { - const generatorDefinition = getGeneratorDefinition(opts.fieldGeneration[opts.typeName][opts.fieldName]); + const generatorDefinition = getGeneratorDefinition( + opts.fieldGeneration[opts.typeName][opts.fieldName], + opts.generatorMode, + ); return getCustomValue(generatorDefinition, opts); } // Check for a general field generation definition if ('_all' in opts.fieldGeneration && opts.fieldName in opts.fieldGeneration['_all']) { - const generatorDefinition = getGeneratorDefinition(opts.fieldGeneration['_all'][opts.fieldName]); + const generatorDefinition = getGeneratorDefinition( + opts.fieldGeneration['_all'][opts.fieldName], + opts.generatorMode, + ); return getCustomValue(generatorDefinition, opts); } } @@ -290,25 +305,36 @@ const getNamedType = (opts: Options): if (!opts.dynamicValues) mockValueGenerator.seed(hashedString(opts.typeName + opts.fieldName)); const name = opts.currentType.name.value; const casedName = createNameConverter(opts.typeNamesConvention, opts.transformUnderscore)(name); + switch (name) { case 'String': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['String']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['String'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.word); } case 'Float': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Float']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['Float'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.float); } case 'ID': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['ID']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['ID'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.uuid); } case 'Boolean': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Boolean']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['Boolean'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.boolean); } case 'Int': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Int']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['Int'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.integer); } default: { @@ -345,7 +371,7 @@ const getNamedType = (opts: Options): }); case 'scalar': { const customScalar = opts.customScalars - ? getGeneratorDefinition(opts.customScalars[foundType.name]) + ? getGeneratorDefinition(opts.customScalars[foundType.name], opts.generatorMode) : null; // it's a scalar, let's use a string as a value if there is no custom @@ -559,9 +585,13 @@ type GeneratorDefinition = { }; }; type GeneratorOptions = GeneratorName | GeneratorDefinition; +type InputOutputGeneratorOptions = { + input: GeneratorOptions; + output: GeneratorOptions; +}; type ScalarMap = { - [name: string]: GeneratorOptions; + [name: string]: GeneratorOptions | InputOutputGeneratorOptions; }; type TypeFieldMap = { @@ -728,6 +758,7 @@ export const plugin: PluginFunction = (schema, docu const value = generateMockValue({ typeName, fieldName, + generatorMode: 'output', currentType: node.type, ...sharedGenerateMockOpts, }); @@ -753,6 +784,7 @@ export const plugin: PluginFunction = (schema, docu typeName: fieldName, fieldName: field.name.value, currentType: field.type, + generatorMode: 'input', ...sharedGenerateMockOpts, }); @@ -764,6 +796,7 @@ export const plugin: PluginFunction = (schema, docu typeName: fieldName, fieldName: field.name.value, currentType: field.type, + generatorMode: 'input', ...sharedGenerateMockOpts, }); From e336c6cde05e937d3fd17af80f16f027354fc493 Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 13:04:08 -0600 Subject: [PATCH 4/9] null-check value in getGeneratorDefinition Tests were failing. Also, assert the last return is a GeneratorDefinition. I have to clean this up, but essentially TS is being unable to narrow this down. An InputOutputGeneratorOptions is a one level deep object with GeneratorOptions, so the recursive call will inevitably trickle down to a base case where we _do not_ have an InputOutputGeneratorOptions. --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8cc66df..ad09f3c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -102,11 +102,11 @@ const getGeneratorDefinition = ( }; } - if ('input' in value && 'output' in value) { + if (value !== undefined && 'input' in value && 'output' in value) { return getGeneratorDefinition(value[generatorMode], generatorMode); } - return value; + return value as GeneratorDefinition; }; const getCasualCustomValue = ( From 1fdde47bf5e5b6b505cc4b94bd20ed24b7be1209 Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 13:06:38 -0600 Subject: [PATCH 5/9] Add example coverage to scalars/spec.ts I.e. the very basic display of what this PR is meant to change. Follow up commits will cover the faker side of things, and possibly other config permutations. --- tests/scalars/__snapshots__/spec.ts.snap | 67 +++++++++++- tests/scalars/spec.ts | 125 +++++++++++++++++++++++ 2 files changed, 188 insertions(+), 4 deletions(-) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 8dbd200..58783ca 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[`custom scalar generation for native and custom types using casual should generate custom scalars for native and custom types using casual 1`] = ` +exports[`Custom scalar generation using casual should generate custom scalars for native and custom types 1`] = ` " export const anA = (overrides?: Partial): A => { return { @@ -27,7 +27,7 @@ export const aC = (overrides?: Partial): C => { " `; -exports[`custom scalar generation for native and custom types using casual should generate dynamic custom scalars for native and custom types using casual 1`] = ` +exports[`Custom scalar generation using casual should generate dynamic custom scalars for native and custom types 1`] = ` "import casual from 'casual'; casual.seed(0); @@ -59,7 +59,66 @@ export const seedMocks = (seed: number) => casual.seed(seed); " `; -exports[`custom scalar generation for native and custom types using faker should generate custom scalars for native and custom types using faker 1`] = ` +exports[`Custom scalar generation using casual with different input/output configurations should generate distinct custom scalars for native and custom input/output types 1`] = ` +" +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis', + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Kelly_Cremin@Turcotte.biz', + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93, + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564, + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'itaque distinctio iure molestias voluptas reprehenderit quos', + }; +}; +" +`; + +exports[`Custom scalar generation using casual with different input/output configurations should generate distinct dynamic custom scalars for native and custom types 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 aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['string'], + }; +}; + +export const seedMocks = (seed: number) => casual.seed(seed); +" +`; + +exports[`custom scalar generation using faker should generate custom scalars for native and custom types 1`] = ` " export const anA = (overrides?: Partial): A => { return { @@ -86,7 +145,7 @@ export const aC = (overrides?: Partial): C => { " `; -exports[`custom scalar generation for native and custom types using faker should generate dynamic custom scalars for native and custom types using faker 1`] = ` +exports[`custom scalar generation using faker should generate dynamic custom scalars for native and custom types 1`] = ` "import { fakerEN as faker } from '@faker-js/faker'; faker.seed(0); diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index 0dcf060..dc07c92 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -98,6 +98,131 @@ describe('Custom scalar generation using casual', () => { expect(result).toMatchSnapshot(); }); + + describe('with different input/output configurations', () => { + it('should generate distinct custom scalars for native and custom input/output types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: { + input: 'string', + output: 'email', + }, + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Kelly_Cremin@Turcotte.biz',", + ); + + // AnyObject in input C (a string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'itaque distinctio iure molestias voluptas reprehenderit quos',", + ); + + expect(result).toMatchSnapshot(); + }); + + it('should generate distinct dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + 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: { + input: 'string', + output: '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]),", + ); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'],", + ); + + // AnyObject in input C (an string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['string'],", + ); + + expect(result).toMatchSnapshot(); + }); + }); }); describe('custom scalar generation using faker', () => { From 9744479805ae40011301071a040520af82c581ab Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 16:27:20 -0600 Subject: [PATCH 6/9] add coverage for faker --- tests/scalars/__snapshots__/spec.ts.snap | 59 +++++++++++ tests/scalars/spec.ts | 123 +++++++++++++++++++++++ 2 files changed, 182 insertions(+) diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 58783ca..e8b9083 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -176,3 +176,62 @@ export const aC = (overrides?: Partial): C => { export const seedMocks = (seed: number) => faker.seed(seed); " `; + +exports[`custom scalar generation using faker with different input/output configurations should generate distinct custom scalars for native and custom input/output types 1`] = ` +" +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.', + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@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, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'vilicus', + }; +}; +" +`; + +exports[`custom scalar generation using faker with different input/output configurations should generate distinct dynamic custom scalars for native and custom types 1`] = ` +"import { fakerEN as faker } from '@faker-js/faker'; + +faker.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\\"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['number']['int'](...[{\\"min\\":-100,\\"max\\":0}]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\\"min\\":-100,\\"max\\":0}]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['lorem']['word'](), + }; +}; + +export const seedMocks = (seed: number) => faker.seed(seed); +" +`; diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index dc07c92..4047d93 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -320,4 +320,127 @@ describe('custom scalar generation using faker', () => { expect(result).toMatchSnapshot(); }); + + describe('with different input/output configurations', () => { + it('should generate distinct custom scalars for native and custom input/output types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0, fractionDigits: 2 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: { + input: 'lorem.word', + output: 'internet.email', + }, + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',", + ); + + // 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,"); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@gmail.com',", + ); + + // AnyObject in input C (a string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'vilicus',", + ); + + expect(result).toMatchSnapshot(); + }); + + it('should generate distinct dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + dynamicValues: true, + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: { + input: 'lorem.word', + output: '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['number']['float'](...[{\"min\":-100,\"max\":0}]),", + ); + + // ID + expect(result).toContain( + "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"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['number']['int'](...[{\"min\":-100,\"max\":0}]),", + ); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](),", + ); + + // AnyObject in input C (a string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['lorem']['word'](),", + ); + + expect(result).toMatchSnapshot(); + }); + }); }); From 941025688ee84b310016307fe6bc9ed1d6dcc64c Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Tue, 1 Apr 2025 16:45:48 -0600 Subject: [PATCH 7/9] update README --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e366dd3..1d6f757 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ keep all GraphQL names as-is. Available case functions in `change-case-all` are `localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst` [See more](https://github.com/btxtiger/change-case-all) -### scalars (`{ [Scalar: string]: GeneratorOptions }`, defaultValue: `undefined`) +### scalars (`{ [Scalar: string]: GeneratorOptions | InputOutputGeneratorOptions }`, defaultValue: `undefined`) 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 @@ -371,6 +371,24 @@ fieldName: # gets translated to casual.integer.toFixed(3) arguments: 3 ``` +### `InputOutputGeneratorOptions` type + +This type is used in the `scalars` option. It allows you to specify different `GeneratorOptions` for `input` and `output` types for +your scalars, in the same way the [typescript-operations](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations#scalars) plugin does. + +So, using the first example of the previous section, you can specify a `string` for your input and a `Date` for your `output`: + +```yaml +plugins: + - typescript-mock-data: + scalars: + Date: + input: date.weekday # Date fields in input objects will be mocked as strings + output: + generator: date.past # Date fields in other GraphQL types will be mocked as JS Dates + arguments: 10 +``` + ## Examples of usage **codegen.yml** From 65bbcadec24c0b729f7d48746d54bf83c5a19b25 Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Wed, 2 Apr 2025 10:40:20 -0600 Subject: [PATCH 8/9] add a type guard for InputOutputGeneratorOptions and rewrite getGeneratorDefinition with that Hopefully this reads a bit better and while it isn't a discriminated union (and while there's room for improvement still), it's at least not a type assertion --- src/index.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index ad09f3c..4811c19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,21 +92,25 @@ const hashedString = (value: string) => { }; const getGeneratorDefinition = ( - value: GeneratorOptions | InputOutputGeneratorOptions, + opts: GeneratorOptions | InputOutputGeneratorOptions, generatorMode: Options['generatorMode'], ): GeneratorDefinition => { - if (typeof value === 'string') { + if (isAnInputOutputGeneratorOptions(opts)) { + return buildGeneratorDefinition(opts[generatorMode]); + } + + return buildGeneratorDefinition(opts); +}; + +const buildGeneratorDefinition = (opts: GeneratorOptions) => { + if (typeof opts === 'string') { return { - generator: value, + generator: opts, arguments: [], }; } - if (value !== undefined && 'input' in value && 'output' in value) { - return getGeneratorDefinition(value[generatorMode], generatorMode); - } - - return value as GeneratorDefinition; + return opts; }; const getCasualCustomValue = ( @@ -590,6 +594,11 @@ type InputOutputGeneratorOptions = { output: GeneratorOptions; }; +const isAnInputOutputGeneratorOptions = ( + opts: GeneratorOptions | InputOutputGeneratorOptions, +): opts is InputOutputGeneratorOptions => + opts !== undefined && typeof opts !== 'string' && 'input' in opts && 'output' in opts; + type ScalarMap = { [name: string]: GeneratorOptions | InputOutputGeneratorOptions; }; From d0ac38375114326a719cd6433f7d50278316fd14 Mon Sep 17 00:00:00 2001 From: Ricardo Vela Date: Wed, 2 Apr 2025 17:18:58 -0600 Subject: [PATCH 9/9] actions/cache@v2 -> @v4 Seems v2 was deprecated on Feb 1st (!) From a quick skim, seems the config remained unchanged, though. Worth a shot! --- .github/workflows/build.yml | 2 +- .github/workflows/deploy.yml | 2 +- .github/workflows/lint.yml | 2 +- .github/workflows/test.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ed37139..4892e91 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7211e8a..124f60e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -25,7 +25,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 061e856..dc43611 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,7 +19,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 340e208..2826479 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}